1

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 know 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.

1
  • Are you still experiencing any difficulty? Commented May 30, 2011 at 22:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

^^ Sorry I missed the outer element wrap, I've included that now!
0

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();

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.