views:

168

answers:

2
<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???

+4  A: 
$.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"));
});
Shadi Almosri
Just to make a note i haven't tested this code, just wrote it out, so you might need to tweak it a little to get the right results :) but it should give you a good basis to understand the answer,
Shadi Almosri
A: 
$('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.

Alan