PDA

View Full Version : Resolved jQuery XML particular node



ggalan
08-15-2011, 11:44 PM
i am parsing xml thru jQuery fine like this


$(document).ready(function(){
$.ajax({
type: "GET",
url: "jquery_xml.xml",
dataType: "xml",
success: parseXml,
error: err
});
});
function err(xhr, reason, ex) {
$('#output').append(reason);
}

function parseXml(xml){
$(xml).find("Tutorial").each(function(){
$("#output_2").append($(this).find("Categories").text() + "<br />");
});

}


xml


<?xml version="1.0" encoding="iso-8859-1"?>
<RecentTutorials>
<Tutorial author="The Reddest">
<Title>Silverlight and the Netflix API</Title>
<Categories>
<Category>Tutorials</Category>
<Category>Silverlight 2.0</Category>
<Category>Silverlight</Category>
<Category>C#</Category>
<Category>XAML</Category>
</Categories>
<Date>1/13/2009</Date>
</Tutorial>
<Tutorial author="The Hairiest">
<Title>Cake PHP 4 - Saving and Validating Data</Title>
<Categories>
<Category>Tutorials</Category>
<Category>CakePHP</Category>
<Category>PHP</Category>
</Categories>
<Date>1/12/2009</Date>
</Tutorial>
<Tutorial author="The Tallest">
<Title>Silverlight 2 - Using initParams</Title>
<Categories>
<Category>Tutorials</Category>
<Category>Silverlight 2.0</Category>
<Category>Silverlight</Category>
<Category>C#</Category>
<Category>HTML</Category>
</Categories>
<Date>1/6/2009</Date>
</Tutorial>
<Tutorial author="The Fattest">
<Title>Controlling iTunes with AutoHotkey</Title>
<Categories>
<Category>Tutorials</Category>
<Category>AutoHotkey</Category>
</Categories>
<Date>12/12/2008</Date>
</Tutorial>
</RecentTutorials>


but this returns ALL the nodes under Categories.
How can i target a particular node under Categories such as Category[1]
so that i output the second entry under this node? i have attached my files
thanks in advance

jscheuer1
08-16-2011, 03:59 AM
If you want all the second Category nodes from each Tutorial node, I'd try:


function parseXml(xml){
$(xml).find("Tutorial").each(function(){
$("#output_2").append($(this).find("Category").eq(1).text() + "<br />");
});
}