jQuery帮助-将XML解析为数组

jQuery帮助-将XML解析为数组,jquery,xml,arrays,http,parsing,Jquery,Xml,Arrays,Http,Parsing,我目前正在开发一个HTML游戏,用户可以拖动项目并将其放入正确的类别中。类别及其项在XML文档中定义 我的XML格式: <config> <game> <title>Dementia</title> <cat> <catTitle>Mild</catTitle> <item>mild-1</item>

我目前正在开发一个HTML游戏,用户可以拖动项目并将其放入正确的类别中。类别及其项在XML文档中定义

我的XML格式:

<config>
<game>
    <title>Dementia</title>
        <cat>
            <catTitle>Mild</catTitle>
            <item>mild-1</item>
            <item>mild-2</item>
            <item>mild-3</item>
        </cat>
        <cat>
            <catTitle>Moderate</catTitle>
            <item>Moderate-1</item>
            <item>Moderate-2</item>
            <item>Moderate-3</item>
        </cat>
        <cat>
            <catTitle>Severe</catTitle>
            <item>Severe-1</item>
            <item>Severe-2</item>
        </cat>
</game>

痴呆
温和的
轻度-1
轻度-2
轻度-3
适度的
中等-1
中等-2
中等-3
严峻的
严重-1
严重-2
我想使用jQuery将这个XML文件解析为基于类别的单独数组

例如:

阵列1=[轻度-1,轻度-2,轻度-3]

阵列2=[中等-1、中等-2、中等-3]等

这将允许我根据category数组检查已删除项的属性是否正确

如果你对如何做到这一点有任何其他想法,请提出建议

提前谢谢你。

试试这样:

$("cat", xml).each(function () {
   $("item", this).toArray();
});
$(document).ready(function() {
    var arr = [];
    $(xml).find("cat").each(function(idx, v) {
        arr[idx] = [];
        $(v).find("item").each(function( i , vi) {
            arr[idx].push( $(vi).text() );
        });             
    });
    console.log( arr );
});
试着这样做:

$("cat", xml).each(function () {
   $("item", this).toArray();
});
$(document).ready(function() {
    var arr = [];
    $(xml).find("cat").each(function(idx, v) {
        arr[idx] = [];
        $(v).find("item").each(function( i , vi) {
            arr[idx].push( $(vi).text() );
        });             
    });
    console.log( arr );
});

试试,像这样:

$("cat", xml).each(function () {
   $("item", this).toArray();
});
$(document).ready(function() {
    var arr = [];
    $(xml).find("cat").each(function(idx, v) {
        arr[idx] = [];
        $(v).find("item").each(function( i , vi) {
            arr[idx].push( $(vi).text() );
        });             
    });
    console.log( arr );
});
将返回如下响应:

[
    ["mild-1", "mild-2", "mild-3"]
, 
    ["Moderate-1", "Moderate-2", "Moderate-3"]
, 
    ["Severe-1", "Severe-2"]
]
因此,您可以访问单个阵列,如下所示:

console.log( arr[0] ); //for first, and so on..

试试,像这样:

$("cat", xml).each(function () {
   $("item", this).toArray();
});
$(document).ready(function() {
    var arr = [];
    $(xml).find("cat").each(function(idx, v) {
        arr[idx] = [];
        $(v).find("item").each(function( i , vi) {
            arr[idx].push( $(vi).text() );
        });             
    });
    console.log( arr );
});
将返回如下响应:

[
    ["mild-1", "mild-2", "mild-3"]
, 
    ["Moderate-1", "Moderate-2", "Moderate-3"]
, 
    ["Severe-1", "Severe-2"]
]
因此,您可以访问单个阵列,如下所示:

console.log( arr[0] ); //for first, and so on..
因为
$(xml)
在IE中不起作用(参见此
jQuery.find()
在IE中不返回数据,但在Firefox和Chrome中返回数据)

因为
$(xml)
在IE中不起作用(参见此
jQuery.find()
在IE中不返回数据,但在Firefox和Chrome中返回数据)