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

时间:2013-01-08 04:01:51

标签: javascript jquery xml jquery-selectors

以下是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()

1 个答案:

答案 0 :(得分:3)

请改用它......

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

您之前的示例是获取对这些元素的引用。这将使用内部文本替换引用。