如何获取XML标记内的内容以创建数组

How to get the contents inside a XML tag to create an array?

本文关键字:数组 创建 何获取 获取 XML      更新时间:2023-09-26

XML文件:

<SketchPad>
<Player>
    <TotalPage>2</TotalPage>
    <BackgroundImage>/Users/ltlab/Library/Application Support/iPhone Simulator/5.1/Applications/D84490FE-3450-456C-A8FE-16BE8B1EB12C/Documents/publicUser_1356864779.441782_local.png</BackgroundImage>
    <Name>1</Name>
    <SelfBackgroundImage>sound.png</SelfBackgroundImage>
    <Type>Record</Type>
    <X_Value>0.000000</X_Value>
    <Y_Value>38.000000</Y_Value>
    <Height_Value>50.000000</Height_Value>
    <Width_Value>50.000000</Width_Value>
    <File_Path>null</File_Path>
</Player>
<Player>
    <TotalPage>2</TotalPage>
    <BackgroundImage>/Users/ltlab/Library/Application Support/iPhone Simulator/5.1/Applications/D84490FE-3450-456C-A8FE-16BE8B1EB12C/Documents/publicUser_1356864779.441782_local.png</BackgroundImage>
    <Name>1</Name>
    <SelfBackgroundImage>8_128x128.png</SelfBackgroundImage>
    <Type>Stamp</Type>
    <X_Value>7.000000</X_Value>
    <Y_Value>716.000000</Y_Value>
    <Height_Value>80.000000</Height_Value>
    <Width_Value>80.000000</Width_Value>
    <File_Path>null</File_Path>
</Player>
<Player>
    <TotalPage>2</TotalPage>
    <BackgroundImage>/Users/ltlab/Library/Application Support/iPhone Simulator/5.1/Applications/D84490FE-3450-456C-A8FE-16BE8B1EB12C/Documents/publicUser_1356864779.441782_local.png</BackgroundImage>
    <Name>1</Name>
    <SelfBackgroundImage>duck.png</SelfBackgroundImage>
    <Type>Stamp</Type>
    <X_Value>570.000000</X_Value>
    <Y_Value>715.000000</Y_Value>
    <Height_Value>80.000000</Height_Value>
    <Width_Value>80.000000</Width_Value>
    <File_Path>null</File_Path>
</Player>
</SketchPad>


我要做的是收集标签内的所有数据。

$(xml).find("X_Value").toArray();

但是它返回的数组仍然包含这样的标签:

[<x_value>0.0000000<x_value>,<x_value>7.0000000<x_value>,<x_value>570.0000000<x_value>]

不是期望的数组:[0.0000000,7.0000000,570.0000000]

我怎样才能直接提取标签内的值,使一个数组?

我真的不知道如何操作jQuery.map()

用这个代替…

$(xml).find("X_Value").map(function() { return $(this).text(); }).get();

前面的例子是获取对这些元素的引用。这将用内部文本替换引用