Javascript 从javascript加载xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8249155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:18:38  来源:igfitidea点击:

Load xml from javascript

javascriptxmlxmlhttprequest

提问by mmvie

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 完全陌生,我一直在为这个非常简单的目标而苦苦挣扎太久(尽管我可以在互联网上找到足够多的相关信息)。只需要这个 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>

In javascript, I use this code to get the xml data:

在 javascript 中,我使用此代码来获取 xml 数据:

// 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..

但是没有结果,因为我没有看到警报..

回答by Richard JP Le Guen

Your server isn't returning the appropriate Content-Typeheader. The responseXMLproperty only works if the server returns a Content-Type: text/xmlor similar +xmlheader.

您的服务器没有返回适当的Content-Type标头。该responseXML属性仅在服务器返回一个Content-Type: text/xml或类似的+xml标头时才有效。

See Ajax Patterns:

请参阅 Ajax 模式

The service just needs to output an XML Content-type header...

该服务只需要输出一个 XML 内容类型的标头...

From the w3c:

从 w3c

If final MIME type is not null, text/xml, application/xml, and does not end in +xml [...] return null.

如果最终 MIME 类型不为 null,则 text/xml、application/xml 且不以 +xml [...] 结尾,则返回 null。

If you have no access to the server and can't change the Content-Typeheader, use the overrideMimeTypefunction to force the XMLHttpRequestto treat the response as text/xml:

如果您无权访问服务器并且无法更改Content-Type标头,请使用该overrideMimeType函数强制XMLHttpRequest将响应视为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.com/2010/11/xmlhttprequestresponsexml-returns-null.html

引文:http: //blog-rat.blogspot.com/2010/11/xmlhttprequestresponsexml-returns-null.html

回答by DrStrangeLove

// 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');

回答by Galled

I make a simple test with a file called price.xml:

我用一个名为 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:

和 html 与此代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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 .textatribute instead .textContent.

并为我工作。我认为你失败了,因为你正在调用.text属性.textContent