1

For example:

<school>
    <list>
        <name>Ali</name>
        <age>19</age>
    </list>
    <list>
        <name>John</name>
        <age>22</age>
    </list>
    <list>
        <name>Marie</name> 
        <age>20</age>
    </list>
    <list>
        <name>Anne</name>
        <age>23</age>
    </list>
</school>

How do I get the age of Marie or Anne?

I can get the age of Ali (with following code) because it is the first element:

var age = $(xml).find('age').first().text();

2 Answers 2

2

Try this if you want to be able to access by name: http://jsfiddle.net/SPu8T/

$(xml).find("name").filter( function() {
    return $(this).text() == "Marie"
}).siblings("age").text();

This if you want to access by index (where i is the index)...

$(xml).find('age').eq(i).text();
Sign up to request clarification or add additional context in comments.

Comments

0

It might be easier to use standard javascript for this.

xml.getElementsByTagName('age')[i].firstChild.nodeValue;

Just change the index represented by i above to get whichever age you want.

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.