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

2

2 Answers 2

4
$.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"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

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,
0
$('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.

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.