21

I try parsing the following type of XML (data from getMembers.php):

<?xml version="1.0" encoding="ISO-8859-1"?>
<members>
   <id>3422345</id>
   <name>Bill Gates</name>
   <id>232311</id>
   <name>Bob Barker</name>
</members>

And I use the code below to parse it:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "getMembers.php",
            cache: false,
            dataType: "xml",
            success: function(xml) {
                $(xml).find('members').each(function(){
                    var name = $(this).find("name").text()
                    alert(name);
                });
            }
        });
    });
</script>

No error but I get both names at the same time.

Like:

Bill GatesBob Barker <--first loop>

Instead of

Bill Gates <--first loop>

Bob Barker <--second loop>

Any help would be great!

2
  • 1
    Is that a typo : <memebers> ? Commented Apr 19, 2012 at 7:59
  • Oops sorry about that. It's been corrected. Commented Apr 19, 2012 at 8:09

3 Answers 3

35

I think you iterate over members, not names:

success: function(xml) {
                $(xml).find('members').each(function(){
                    $(this).find("name").each(function(){
                        var name = $(this).text();
                        alert(name);
                    });
                });
            }

Or maybe your XML should looks like:

<members>
   <id>3422345</id>
   <name>Bill Gates</name>
</members>
<members>
   <id>232311</id>
   <name>Bob Barker</name>
</members>
Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Thanks fmgp! :o)
i also recommend to use the following valid xml format as seen in the answer from user2780947: <members> <member> <id>123</id> <name>First Last</name> </member> <member> <id>124</id> <name>Bill Gates</name> </member> </members>
2

This way show names:

<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "getMembers.php",
        cache: false,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('name').each(function(){
                        var name = $(this).text();
                        alert(name);
            });
        }
    });
});

Comments

2

Just wondering if you successfully parsed that XML data.

The valid XML format is below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<members>
  <member>
   <id>3422345</id>
   <name>Bill Gates</name>
  </member>
  <member>
   <id>232311</id>
   <name>Bob Barker</name>
 </member>
</members>

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.