Hamutaro
Hamutaro

Reputation: 21

Jquery retrieve XML data with same node

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

Upvotes: 1

Views: 174

Answers (2)

Smern
Smern

Reputation: 19076

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

Upvotes: 2

Jnatalzia
Jnatalzia

Reputation: 1687

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.

Upvotes: 0

Related Questions