push xml values in to an array using javascript -
i have 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 push 'web-portal-urls' in associative array(key/value). requrement is, when user clicks on image, show assosiative 'webprotal-gallery' in page.
how push gallery items in array.
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 = 0; < tags.length; i++){ urls.push(tags[i].textcontent) } console.log(urls)
demo: fiddle
Comments
Post a Comment