0

I have the XML file,

<root>
<items>
 <webprotal-gallery>
  <web-portal-urls>http://xxx.xom/image</web-portal-urls>
  <web-portal-urls>http://xxx.xom/image</web-portal-urls>
  <-----   -----------    ---------------   ------------>
 </webprotal-gallery> 
</items>
<items>
<webprotal-gallery>
  <web-portal-urls>http://xxx.xom/image</web-portal-urls>
  <web-portal-urls>http://xxx.xom/image</web-portal-urls>     
 </webprotal-gallery>
</items>
</root>

I want to push 'web-portal-urls' in to an associative array(key/value). My requrement is, When the user clicks on image, to show assosiative 'webprotal-gallery' in another page.

How to push gallery items in to an array.

2

1 Answer 1

1

Try

function parseXml(xml){
    var xmlDoc, parser;

    if (window.DOMParser){
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(xml,"text/xml");
    } else {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.loadXML(xml); 
    }

    return xmlDoc;
}

var xml = '<root><items><webprotal-gallery><web-portal-urls>http://xxx.xom/image</web-portal-urls><web-portal-urls>http://xxx.xom/image</web-portal-urls></webprotal-gallery></items><items><webprotal-gallery><web-portal-urls>http://xxx.xom/image</web-portal-urls><web-portal-urls>http://xxx.xom/image</web-portal-urls></webprotal-gallery></items></root>';

var xmlDoc = parseXml(xml);
var urls = [];
var tags = xmlDoc.getElementsByTagName('web-portal-urls');

for(var i = 0; i < tags.length; i++){
    urls.push(tags[i].textContent)
}

console.log(urls)

Demo: Fiddle

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

1 Comment

hi, thank u for u r reply, But i need output like this, gallery1{"image1","image2"},gallery2{"image1","image2"}... so that when the user clicks on the image, i can show all the gallery items to that image

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.