XML parsing in jQuery - 3rd level nodes



I have an XML like this:



<?xml version="1.0"?>
<service>
<name>My Element</name>
<header>My Header</header>
<sections>
<section>
<!-- Optional -->
<subheader>Subheader 1</subheader>
<description>Here goes the content</description>
</section>
<!-- Optional -->
<section>
<subheader>Subheader 2</subheader>
<description>Here goes the content</description>
</section>
</sections>
</service>


And I am trying to read the values of the "subheader" and "description" element from each section. However I am unable to retrieve the values of these elements. Here is my code:



function getDescription(descriptor_url) {
var descriptor_object = $.get(descriptor_url, function(descriptor_data, descriptor_status, descriptor_response) {
$(descriptor_response.responseXML).each(function () {
//this works fine
var header = $(this).find('header').text();
});
$(descriptor_response.responseXML).find('sections').each(function() {
// here is where the issue is
$(this).find('section').each(function() {
var subheader = $(this).find('subheader').text;
alert(subheader);
})
})
})
.fail(function() {
response.value = "Something went wrong with the request.\n\nReason: " + descriptor_object.statusText;
})
}


I was thinking that $(this) is local to $.each loop but looks like i'm wrong. Can someone please help?


Thanks!


alert message when i try to print the subheader - looks like a jquery function


No comments:

Post a Comment