views:

417

answers:

1

I have an XML file which has four <resutGroups> tag:

<resultGroups>
  <subGroups>
    <name> </name>
  </subGroups>
  <name> </name>
</resultGroups>

each <resultGroup> has several <subGroups> and each <subGroups> has <name> tag.

I want to select only the name tag of <resultGroups> only

$(xml).find("resultGroups").each(function() {
  alert( $(this).find("name").text() ); 
}

When I use the above code it returns all the names inside the <resultgroups> and <subGroups>.

How can I select only one <name> which is in the <resultGroups> tag?

+6  A: 

You have a couple of options:

var xml = $(xml);
$('resultGroups > name', xml).each(function() {
    alert($(this).text());
});

This uses the direct descendant selector. You could also use children, which does the same thing:

$('resultGroups', xml).children('name').each(function() {
    alert($(this).text());
});
Paolo Bergantino
Thnx for tht .... it works....i was banging my head for last one hour.. :-)
Jasim