I have an XML like this
<resultGroups>
<subGroups>
<results> </results>
<results> </results>
</subGroups>
<subGroups>
<results> </results>
<results> </results>
</subGroups>
<name> </name>
</resultGroups>
<resultGroups>
<subGroups>
<results> </results>
<results> </results>
</subGroups>
<subGroups>
<results> </results>
<results> </results>
</subGroups>
<name> </name>
</resultGroups>
I have this code to select number of results inside each resultGroup
$('resultGroups', $(xml)).each(function() {
count = $('results', this).length;
arr[i] = count;
i++;
});
What I want is to get the name from each resultGroup tag and store it in an array.
How can I do that inside the above code?
How can I select only the name tag of the first resultGroup only without .each function?
If your XML looks like this:
Using this code:
After that, groups will be a dictionary (object, hence the {}) like so:
Which you can access by doing
groups.mynameorgroups['myname']To get only the name tags directly in resultGroups tag – I didn’t see any other in the example code, so I didn’t know there were more – use this code instead: