Andromeda
Andromeda

Reputation: 12897

XML parsing with Jquery

<resultGroups> 
    <subGroups>
        <results> </results>
        <name></name>
    </subGroups>
    <subGroups>
        <results> </results>
        <name></name>
    </subGroups>    
    <name>myname</name>
</resultGroups>
<resultGroups> 
    <subGroups>
        <results> </results>
        <results> </results>
        <name></name>
    </subGroups>
    <subGroups>
        <results> </results>
        <results> </results>
        <name></name>
    </subGroups>    
    <name>othername</name>
</resultGroups>

how can i select the name tag of first resultGroup only using jquery???

Upvotes: 1

Views: 288

Answers (2)

Shadi Almosri
Shadi Almosri

Reputation: 11989

$.ajax({
    url: "result.xml",
    dataType: "xml",
    success: function(data){
        // Parsing happens here

    }
 });

To parse the document we have two main functions available "each" and "find". The function "each" will loop over all the tags with a specific name, the function "find" will search for a certain tag.

$(data).find("resultGroup").each(function() {
    alert("found a resultGroup");
});


$(data).find("resultGroup").each(function() {
        alert($(this).find("subgroups").text());
});


$(data).find("subgroups").each(function() {
        alert($(this).attr("result"));
});

Upvotes: 4

Alan Plum
Alan Plum

Reputation: 10902

$('resultGroup subGroups name')[0]

If you want to be more specific, you could split the selection and fetch the first array index for each:

$('name', $('subGroups', $('resultGroup')[0])[0])

... I think.

Upvotes: 0

Related Questions