0

I have to parse the below XML :

var xml = <gallery>
<image full="/images/gallery-resort/Sole_East_Lobby.jpg"/>
<image full="/images/gallery-resort/Cool_Crisp.jpg"/>
<image full="/images/gallery-resort/07_AK_Sole_Lobby2.jpg"/>
</gallery>

I want get the values of full attribute from the image tag. This structure is not proper but I need these image paths to show the images. I have tried below code for this but didn't worked:

xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ); 
$($xml).each(function(){
var v = $(this).find("gallery");
});

Please, check it folks.

1 Answer 1

1

A simple example that would create an array of the url's would be:

  var imgs =  $xml.find('image').map(function(){
     return $(this).attr('full');
  }).get();

DEMO

Or you could use each and add a new element for every iteration in the loop

$xml.find('image').each(function(){
    $('<img>',{src: $(this).attr('full')}).appendTo('body');   
});
Sign up to request clarification or add additional context in comments.

1 Comment

@ charlietfl thanks for your comment. I am trying to read this array of image using the following code var farr = JSON.stringify(imgs,null,' '); for (i = 0; i < farr.length; i++) { alert(farr[i]); } But its read character by character instead of reading the complete image path on index of imgs variable. I have used JSON.stringify(imgs,null,' ') to decode the JSON to array but didn't worked. You can see response here: http://dev.soleeast.com/parseXML2.php Please, advice.

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.