jQuery的帮助 - 解析XML到数组 [英] jQuery help - Parse XML into array

查看:100
本文介绍了jQuery的帮助 - 解析XML到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个HTML游戏由此用户拖动项目并将它们落入他们正确的类别。的类别及其项都在XML文档中定义

I am currently developing a HTML game whereby a user drags items and drops them into their correct categories. The categories and their items are defined in an XML document.

我的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>

我想分析使用jQuery到根据其类别单独阵列此XML文件。

I want to parse this XML file using jQuery into seperate arrays based on their category.

因此​​,例如:

ARRAY1 = [温和-1,轻度-2,轻度-3]

array1 = [mild-1,mild-2,mild-3]

数组2 =中等-1,中度2,中度3]等...

array2 = [Moderate-1,Moderate-2,Moderate-3] etc...

这将让我检查,如果基于类别阵列上放置项目的属性是正确的。

This would allow me to check if the dropped item's attribute is correct based on the category array.

如果你有怎样可以这样做请不要提出任何其他的想法。

If you have any other ideas of how this could be done please do suggest.

感谢您提前。

推荐答案

尝试,是这样的:

$(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 );
});

将返回响应,如:

Will return response like:

[
    ["mild-1", "mild-2", "mild-3"]
, 
    ["Moderate-1", "Moderate-2", "Moderate-3"]
, 
    ["Severe-1", "Severe-2"]
]

所以,你可以访问各个阵列,

So you can access individual array as,

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

这篇关于jQuery的帮助 - 解析XML到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆