[英]XML Parsing in Javascript
I'm trying to parse an xml coming from an XMLHttpRequest (for a Firefox extension). 我正在尝试解析来自XMLHttpRequest的xml(用于Firefox扩展)。 In the following code, req is an XMLHttpRequest object. 在以下代码中,req是XMLHttpRequest对象。 I did req.overrideMimeType("text/xml");
我做了req.overrideMimeType("text/xml");
after declaring req. 宣布要求后。
var shortURL;
var xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async = false;
xmlDoc = req.responseXml;
if (xmlDoc.readyState == 4){
shortURL = xmlDoc.documentElement.childNodes[8].text;
}
If I use req.responseXml
I get an error saying "xmlDoc is not declared" for the line after xmlDoc = req.responseXml;
如果我使用req.responseXml
,则在xmlDoc = req.responseXml;
之后的行会出现一条错误消息,指出“未声明xmlDoc” xmlDoc = req.responseXml;
If I use req.responseText
, xmlDoc.readyState == 4
turns false. 如果我使用req.responseText
,则xmlDoc.readyState == 4
变为false。
I don't do much of javascript so please tell me if I'm doing something wrong here. 我没有做太多的javascript,所以请告诉我我在这里做错了什么。
I generally prefer using responseText
and then parsing the XML using the browser's built in XML parsing library. 我通常更喜欢使用responseText
,然后使用浏览器的内置XML解析库解析XML。 After that, I generally convert the resulting XML document tree, or a sub tree, to JSON for easy access in JavaScript. 之后,通常将生成的XML文档树或子树转换为JSON,以便在JavaScript中轻松访问。
I wrote a tiny utility library for this here: 我在这里为此编写了一个微型实用程序库:
http://earth-api-samples.googlecode.com/svn/trunk/demos/myearth/lib/xmlutil.js http://earth-api-samples.googlecode.com/svn/trunk/demos/myearth/lib/xmlutil.js
The usage is pretty simple: 用法很简单:
var json = xmlNodeToJson(parseXml(req.responseText);
Hai chanux, Hai chanux,
May this will help you to know basics of xml parser 可能这将帮助您了解xml解析器的基础
http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript
AJAX responseXML errors AJAX responseXML错误
or try changing 或尝试改变
shortURL = xmlDoc.documentElement.childNodes[8].Text;
to 至
shortURL = xmlDoc.documentElement.childNodes[8].firstChild.nodeValue;
or use this function and change it for yours... 或使用此功能并为您更改...
function parseXML()
{
if (xmlDoc.readyState == 4 && xmlDoc.status == 200)
{
xmlDoc = xmlDoc.responseXML;
regions = xmlDoc.getElementsByTagName("region");
for (var i = 0; i < regions.length; i++)
{
if (regions[i].getAttribute("id") == regID)
{
var browserName = navigator.userAgent;
var isIE = browserName.match(/MSIE/);
if (isIE)
{
var hotelprice = regions[i].childNodes[0].firstChild.nodeValue;
var pkgprice = regions[i].childNodes[1].firstChild.nodeValue;
}
else
{
var hotelprice = regions[i].childNodes[1].textContent;
var pkgprice = regions[i].childNodes[3].textContent;
}
document.getElementById("hotel").innerHTML = "$"+hotelprice;
document.getElementById("package").innerHTML = "$"+pkgprice;
}
}
}
}
Do you need to use the DOM? 您需要使用DOM吗? If not, use E4X . 如果不是,请使用E4X 。 It's as simple as 就这么简单
shortURL = new XML(req.responseText).child(8).text();
If the response includes an XML declaration ( <?xml version="...">
), use this instead: 如果响应包含XML声明( <?xml version="...">
),请改用此声明:
shortURL = new XML(req.responseText.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "")).child(8).text();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.