从javascript加载xml [英] Load xml from javascript

查看:99
本文介绍了从javascript加载xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全不熟悉XML,我一直在努力解决这个非常简单的目标(尽管我可以在互联网上找到足够的内容)。只需要这个xml文件中的值:

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>

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

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

推荐答案

您的服务器未返回相应的 Content-Type 标头。 responseXML 属性仅在服务器返回 Content-Type:text / xml 或类似 + xml 标题。

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.

请参阅Ajax模式


该服务只需输出XML Content-type标头......

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

来自w3c


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

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

如果您无法访问服务器且无法更改 Content-Type 标头,使用 overrideMimeType 函数强制 XMLHttpRequest 将响应视为 text / xml

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

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

这篇关于从javascript加载xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆