从 javascript 加载 xml

发布于 2024-12-17 20:21:11 字数 1001 浏览 0 评论 0原文

我对 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>

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-12-24 20:21:11

您的服务器未返回适当的 Content-Type 标头。仅当服务器返回 Content-Type: text/xml 或类似的 +xml 标头时,responseXML 属性才起作用。

查看 Ajax 模式

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

来自w3c

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

如果您无权访问服务器并且无法更改 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');

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

Your 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.com/2010/11/xmlhttprequestresponsexml-returns-null.html

得不到的就毁灭 2024-12-24 20:21:11
// 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');
// 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');
2024-12-24 20:21:11

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

并使用以下代码进行了 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>

并且对我有用。我认为您失败了,因为您正在调用 .text 属性而不是 .textContent

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.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 .text atribute instead .textContent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文