0

我需要一点帮助来解析我的 XML。XML如下:

    <?xml version="1.0"?>
<config>
    <game title="Dementia">
            <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>

    <game title="Risks">
        <cat>
            <catTitle>Low</catTitle>
            <item>Low-1</item>
            <item>Low-2</item>
            <item>Low-3</item>
        </cat>
        <cat>
            <catTitle>Medium</catTitle>
            <item>Medium-1</item>
            <item>Medium-2</item>
            <item>Medium-3</item>
        </cat>
        <cat>
            <catTitle>High</catTitle>
            <item>High-1</item>
            <item>High-2</item>
        </cat>
    </game>

</config>

正如您在此配置文件中看到的,有两个“游戏”。每个都有自己的标题属性。我设法选择了这些属性并将它们添加到一个下拉菜单中,用户可以从中选择他们想要玩的游戏。

我的问题是,目前当我解析文档时,它会解析两种游戏,而不仅仅是我想要的游戏。

目前我的代码是:

$(xml).find("cat").each(function(idx, v) {
        categoryArrays[idx] = [];

        $(v).find("catTitle").each(function(){

            //$(xml).find('title:contains("Dimentia")').each(function(){
            numberofCategories = numberofCategories+1;
            tmpCategory = $(this).text();
            categoryNames.push(tmpCategory);


            var $cat = $('<div id=catPileBox>' +
                            '<div class="catPileBoxTitle font">' + tmpCategory + '</div>' +
                            '<div class="catPileBoxBody font"> </div>' +
                        '</div>')
                    .fadeIn('slow').appendTo('#catPile').attr('categoryName', tmpCategory).attr('categoryNum', idx).droppable({
                containment: '#content',
                stack: '#catPileBox',
                cursor: 'move',
                drop: handleDropEvent
            });
        });



        //ITEM PARSING
            $(v).find("item").each(function( i , vi) {
                categoryArrays[idx].push( $(vi).text() );

                numberofItems = numberofItems+1;
                tmpItem = $(this).text();

                //Add Item to Array
                itemNames.push(tmpItem);

                       //UI CREATION and Atrribute setting
                        var $item = //$('<div id=itemPileBox div class=font >' + tmpItem + '</div>')


                $('<div id=itemPileBox>' +
                        '<div class="itemPileBoxPic"><img src="icon.jpg"></div>'+
                        '<div class="itemPileBoxTitle font">' + tmpItem + '</div>' +
                        '<div class="itemPileBoxBody font">Having a fit and going mental</div>' +
                        '</div>')

                                .fadeIn('slow').appendTo('#itemPile').attr('itemName', tmpItem).attr('categoryNum', categoryArrayNum).draggable({
                            containment: '#content',
                            stack: '#itemPileBox',
                            cursor: 'move',
                            revert: true
                        });

                        console.log("Item Created: " + tmpItem + " // Item's Category:" + categoryArrayNum) ;

            });


        console.log('-- Category Array Num: ' + categoryArrayNum + ' contains: ' + categoryArrays[categoryArrayNum]);
        categoryArrayNum++;
        });

      $(shuffleItems());
      console.log("// ITEMS ASSIGNED TO CATEGORIES");

    }

我知道,在开始时,我需要添加一些内容,以查找与组合框中选择的标题匹配的游戏属性。

如果有人能告诉我如何选择或有选择地解析文档中“游戏标题=痴呆”的部分并忽略任何其他游戏(及其孩子),我可以更改所有变量以匹配组合框中的变量。 .

感谢您的时间,并提前感谢!

4

2 回答 2

1

假设您有一个 XMLDoc,您可以这样做:

$(xml).find("game[title=Dementia] cat").each(...

获取具有标题的cat元素下的所有元素。gameDementia

于 2012-09-13T22:05:27.967 回答
1

试试这个代码:

var xmlDoc = $.parseXML('<?xml version="1.0"?><config>     <game title="Dementia">             <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>      <game title="Risks">         <cat>             <catTitle>Low</catTitle>             <item>Low-1</item>             <item>Low-2</item>             <item>Low-3</item>         </cat>         <cat>             <catTitle>Medium</catTitle>             <item>Medium-1</item>             <item>Medium-2</item>             <item>Medium-3</item>         </cat>         <cat>             <catTitle>High</catTitle>             <item>High-1</item>             <item>High-2</item>         </cat>     </game>  </config>' )
console.log($(xmlDoc ).find("game[title=Dementia]"))​

这是jsfiddle

于 2012-09-13T22:08:16.707 回答