XML解析javascript无法正常工作

问题描述:

我正在使用以下代码解析iOS中的本地内存中的xml文件。我一次正确地完成了相同的过程,但现在发生了错误。XML解析javascript无法正常工作

请检查我的代码。

在第一个警报中,我可以看到本地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); 

而底部警报打印空值而不是某些对象对象值。

+0

这个问题可能在你的xml文件中,你确定它是一个格式良好的xml文件吗? –

+0

是的,我确定.... – Bangalore

+0

你有没有试过'xhttp.responseText'? –

如果您要使用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。

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