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?

1 Answer 1

8

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());
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.