XML解析javascript无法正常工作

时间:2014-01-14 09:13:46

标签: javascript html ios cordova xml-parsing

我正在使用以下代码从iOS中的本地内存中解析xml文件。我已经完成了相同的过程,但现在发生了错误。

请检查我的代码。

在第一个警报中,我可以看到本地xml文件的路径(/ Users / NAVEEN / Library / Application Support / iPhone Simulator / 7.0.3 / Applications / F7E7F34A-93A4-4BFF-B366-E8D39C008C52 / Documents / config。 XML)

alert(configFileUrl);

if (window.XMLHttpRequest) {
    xhttp=new XMLHttpRequest();
} else {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xhttp.open("GET",configFileUrl,false);
xhttp.send();

newXMLObj=xhttp.responseXML;

alert(newXMLObj);

底部警告打印null而不是某些Object对象值。

2 个答案:

答案 0 :(得分:0)

如果您要使用xhttp.responseXML,您的回复必须是有效的XML内容类型text/xmlapplication/xml

您可以使用xhttp.responseXML代替xhttp.responseText并在javascript中创建XML文档:

if (window.DOMParser) {
  var parser=new DOMParser();
  var xmlDoc=parser.parseFromString(xhttp.responseText,"text/xml");
}

并且ActiveXObject检查也是不必要的,因为iOS上没有ActiveXObject。

答案 1 :(得分:0)

可能是因为您尝试使用本地文件系统上的JavaScript访问xml文件。