Duplicate Jquery XML parsing
Struggling to figure this out.
I've got an XML file that I call with ajax, then I need to set the element text as variables.
Two of the elements have the same name, and I don't kno开发者_C百科w how to get them separately:
<myElement>
<Country>
<Id>1</Id>
<CountryCode>UK</CountryCode>
**<Name>United Kingdom</Name>**
</Country>
<County>
<Id>7</Id>
**<Name>West Midlands</Name>**
</County>
</myElement>
This is how I currently get them:
$(results).find("myElement").each(function (i, item) {
var countryName = $(this).find('Name').text();
var countyName = **$(this).find(' ???? ').text();**
});
Not come across this before, but the XML file ISN'T mine so I can't just rename the elements.
Well, your county is outside of your country tag, so therefore you'd have to do something like this for that line.
$('County', results).children('Name').each(function(){
countyName = $(this).text();
});
This link further expands upon the above code, and describes it a bit more fully.
I did similar to the above, when there was a duplicate I did this:
$('Country > Name', results).each(function () {
countryName = $(this).text();
});
Where there wasn't I did this:
mainText = $(this).find('MainText').text();
精彩评论