admin管理员组文章数量:1392099
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!!
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!!
Share Improve this question asked Mar 8, 2012 at 23:59 OrlandoOrlando 9752 gold badges20 silver badges45 bronze badges 1- Just figured it out was easier than i thought... heres the code: /* On Category Change */ $(".category").change(function(){ var xml = xmlArtists.getXml(); $(".genre").find("option").remove().end(); var type = $(this).val(); $(xml).find("category[type='" + type + "']").find("genre").each(function(){ var genre = $(this).text(); $(".genre").append("<option value=" + genre + ">" + genre + "</option>"); }); }); – Orlando Commented Mar 9, 2012 at 0:04
1 Answer
Reset to default 4$(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.
本文标签: javascriptjQueryXMLReading child nodes from a specific nodeStack Overflow
版权声明:本文标题:javascript - jQuery : XML - Reading child nodes from a specific node - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744779926a2624645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论