Possible Duplicate:
how to extract values from an XML document using Javascript可能重复:如何使用Javascript从XML文档中提取值
My demo.xml file contain the following data:
我的demo.xml文件包含以下数据:
<?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....
我想使用Javascript将文件demo.xml中的所有这些数据提取到我的代码中。我怎么能得到这个?有解决方案吗谢谢....
1 个解决方案
#1
13
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);
}
}
#1
13
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);
}
}