Reputation: 975
I have an xml file in which i want to read the child nodes of a specific node... Below is the XML file.
The XML file structure:
<?xml version="1.0" encoding="ISO-8859-1"?>
<categories>
<category type='Type A'>
<genre>1</genre>
<genre>2</genre>
</category>
<category type='Type B'>
<genre>3</genre>
<genre>4</genre>
<genre>5</genre>
</category>
<category type='Type C'>
<genre>6</genre>
</category>
</categories>
An example of what i wouild need using this XML is all the genres for category type B. How exactly could i filter out all the other categories so i could do a .find("genre").each.... and return only genres 3,4 and 5.
Heres the code im using:
$.ajax({
type: "GET",
url: "myxml.xml",
dataType: "xml",
success: function(data){
xml = data;
});
}
});
return {getXml : function()
{
if (xml) return xml;
}};
})();
/* On Category Change */
$(".category").change(function(){
var xml = xmlArtists.getXml();
$(".genre").find("option").remove().end();
var type = $(this).val();
var typeGenres = $(xml).find("categories").filter(function(){
return $(this).find("category").attr("type") == type;
});
typeGenres.find("genre").each(function(){
var genre = $(this).text();
$(".genre").append("<option value=" + genre + ">" + genre + "</option>");
});
});
The .category is an object when its changed the code should fire... since im poulating another object upon its change but anyways thats not much of importance for this question...
Thanks in advance!!
Upvotes: 1
Views: 9057
Reputation: 141879
$(xml).find('category[type="Type B"] > genre')
This would find all category
nodes whose type
is "Type B"
and get all the genre
child nodes of each matched category.
Upvotes: 4