0

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>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should refer to xml2json plugin. This is already discussed in [link] (stackoverflow.com/questions/3642789/…)
0

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>

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.