XML parse selected elements using jQuery

fredericko

New Member
I nee a little bit of help in parsing my XML. The XML is as follows:\[code\] <?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>\[/code\]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:\[code\]$(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="http://stackoverflow.com/questions/12415309/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"); }\[/code\]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!
 
Back
Top