admin管理员组文章数量:1287484
Totally new to XML and I've been struggling on this very simple objective for too long (though I can find enough on the internet about it). Just need the values out of this xml file:
<?xml version="1.0" encoding="UTF-8"?>
<materials>
<basic>
<uurloon>10</uurloon>
<setloon>100</setloon>
</basic>
<extra>
<geluid>150</geluid>
<ledset>35</ledset>
<strobo>20</strobo>
<laser>50</laser>
</extra>
</materials>
In javascript, I use this code to get the xml data:
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "pricing.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
alert('end');
No result though, cause I'm not seeing the alert..
Totally new to XML and I've been struggling on this very simple objective for too long (though I can find enough on the internet about it). Just need the values out of this xml file:
<?xml version="1.0" encoding="UTF-8"?>
<materials>
<basic>
<uurloon>10</uurloon>
<setloon>100</setloon>
</basic>
<extra>
<geluid>150</geluid>
<ledset>35</ledset>
<strobo>20</strobo>
<laser>50</laser>
</extra>
</materials>
In javascript, I use this code to get the xml data:
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "pricing.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
alert('end');
No result though, cause I'm not seeing the alert..
Share Improve this question edited Nov 23, 2011 at 21:21 Richard JP Le Guen 28.8k8 gold badges93 silver badges120 bronze badges asked Nov 23, 2011 at 21:14 mmviemmvie 2,5817 gold badges25 silver badges39 bronze badges 03 Answers
Reset to default 2Your server isn't returning the appropriate Content-Type
header. The responseXML
property only works if the server returns a Content-Type: text/xml
or similar +xml
header.
See Ajax Patterns:
The service just needs to output an XML Content-type header...
From the w3c:
If final MIME type is not null, text/xml, application/xml, and does not end in +xml [...] return null.
If you have no access to the server and can't change the Content-Type
header, use the overrideMimeType
function to force the XMLHttpRequest
to treat the response as text/xml
:
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.overrideMimeType('text/xml');
xhttp.open("GET", "pricing.xml", false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
alert('end');
citation: http://blog-rat.blogspot./2010/11/xmlhttprequestresponsexml-returns-null.html
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "pricing.xml", false);
xhttp.send(null);
xhttp.onreadystatechange = function(){
if (xhttp.status == "200")
xmlDoc = xhttp.responseXML;
}
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].textContent;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].textContent;
alert('end');
I make a simple test with a file called price.xml:
<?xml version="1.0" encoding="UTF-8"?>
<materials>
<basic>
<uurloon>10</uurloon>
<setloon>100</setloon>
</basic>
<extra>
<geluid>150</geluid>
<ledset>35</ledset>
<strobo>20</strobo>
<laser>50</laser>
</extra>
</materials>
And html with this code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" dir="ltr" lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body onload="init()">
<p>Hola</p>
<script>
function init(){
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "price.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].textContent;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].textContent;
console.log(uurloon,setloon); //give me "10 100"
}
</script>
</body>
</html>
And in works for me. I think fails for you because you are calling the .text
atribute instead .textContent
.
本文标签: Load xml from javascriptStack Overflow
版权声明:本文标题:Load xml from javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741312329a2371717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论