Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve xml data from Javascript [duplicate]

Possible Duplicate:
how to extract values from an XML document using Javascript

My demo.xml file contain the following data:

<?xml version="1.0"?>
<user>
<details>
<name>abc</name>
<class>xyz</class>
<city>pqr</city>
</details>
<info>
<id>123</id>
<code>456</code>
</info>
</user>

I want to fetch all these data from file demo.xml into my code using Javascript. How can I get this? Any solution? Thanks....

like image 936
J.K.A. Avatar asked May 21 '12 11:05

J.K.A.


1 Answers

here is an example wich can put you in the right direction:

var request = new XMLHttpRequest();
request.open("GET", "/path/demo.xml", false);
request.send();
var xml = request.responseXML;
var users = xml.getElementsByTagName("user");
for(var i = 0; i < users.length; i++) {
    var user = users[i];
    var names = user.getElementsByTagName("name");
    for(var j = 0; j < names.length; j++) {
        alert(names[j].childNodes[0].nodeValue);
    }
}
like image 117
Tom Avatar answered Nov 16 '22 10:11

Tom