0

I nee a little bit of help in parsing my XML. The XML is as follows:

    <?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>

As you can see in this config file there are two "Games". Each has their own title attribute. I have managed to select these attributes and add them to a drop down menu from which the user selects which game they want to play.

My problem is that currently when I parse the document, it parses through both games, rather than just the one that I want.

At the moment my code is:

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

    }

I understand that at the start I will need to add something to do with find the game attribute where title matches that selected in the combo box.

If someone could just tell me how I can select or selectively parse just the part of the document where "game title=Dementia" and ignore any other game(and their children) I can change all the variables to match those in the combobox myself..

Thanks for your time, and thanks in advance!

1
  • where is xml coming from, has it been parsed to an xml document? Commented Sep 13, 2012 at 21:59

2 Answers 2

1

Try this code:

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]"))​

Here is jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have an XMLDoc, you could do this:

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

to get all cat elements under game elements that have the title Dementia.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.