StealthRT
StealthRT

Reputation: 10542

ajax jquery XML return looping

Hey all i am currently using this code below to gather my returned XML data:

success: function(xml) {
    $(xml).find('members').each(function(){ 
       $(this).find("id").each(function(){        
         var id = $(this).text();
         $("#example").append('<img src="http//www.xxxxxxx.com/' + id + '.jpg">');
       }); 
    }); 
}

However, i need it to be able to gather ALL the data from each section at a time without having to loop just for one thing then go back and loop to get the second, etc etc.

The XML looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<members>
  <new>YES</new>
  <id>5678994</id>
  <name>Bob Barker</name>
  <rsvp>0</rsvp>
  <new>NO</new>
  <id>94443326</id>
  <name>Bill Gates</name>
  <rsvp>0</rsvp>
  <street1>na</street1>
  <street2>na</street2>
  <city>na</city>
  <state>na</state>
  <zip>0</zip>
  <cellp>0</cellp>
</members>

Upvotes: 0

Views: 265

Answers (2)

StealthRT
StealthRT

Reputation: 10542

Got it:

$(xml).find('members').each(function(){ 
    $(this).find("user").each(function(){ 
        var found   = $(this).find("new").text();
        var id      = $(this).find("id").text();
        var name    = $(this).find("name").text();
        var rsvp    = $(this).find("rsvp").text();
        var street1 = $(this).find("street1").text();
        var street2 = $(this).find("street2").text();
        var city    = $(this).find("city").text();
        var state   = $(this).find("state").text();
        var zip     = $(this).find("zip").text();
        var cellp   = $(this).find("cellp").text();

        alert(street1);
        alert(id);
    }); 
}); 

using the xml layout of:

<?xml version="1.0" encoding="ISO-8859-1"?>
<members>
<user>
<new>NO</new>
<id>5000834</id>
<name>bob Barker</name>
<rsvp>0</rsvp>
<street1>na</street1>
<street2>na</street2>
<city>na</city>
<state>na</state>
<zip>0</zip>
<cellp>0</cellp>
</user>
<user>
<new>NO</new>
<id>94323456</id>
<name>Bill Gates</name>
<rsvp>0</rsvp>
<street1>na</street1>
<street2>na</street2>
<city>na</city>
<state>na</state>
<zip>0</zip>
<cellp>0</cellp>
</user>
</members>

Upvotes: 0

Ahamed Mustafa M
Ahamed Mustafa M

Reputation: 3139

You can simplify the traversing like

$divToAppend=$("#example");    
$(xml).find("members id").each(function(){
    $divToAppend.append('<img src="http//www.xxxxxxx.com/' + $(this).text() + '.jpg" />');
 });

Div / Element '#example' is cached for better performance.

Upvotes: 1

Related Questions