【发布时间】:2013-03-09 12:05:08
【问题描述】:
我正在开发一个应用程序,点击一个按钮,程序会调用存储在 XML 文件中的信息,以使用 JavaScript 显示在 <span> 标记中;
function viewXMLFiles() {
console.log("viewXMLFiles() is running");
var xmlhttp = new HttpRequest();
xmlhttp.open("GET", "TestInfo.xml", false);
xmlhttp.send;
console.log("still running");
var xmlDoc = xmlhttp.responseXML;
console.log("getting tired");
document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[0].childNodes[0].nodeValue;
document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[1].childNodes[1].nodeValue;
console.log("done");
}
然后是调用它的 HTML(以及显示 XML 文件的位置);
<button onclick = "viewXMLFiles();">View Document Info</button><br>
<span id = "documentList">
<!--This is where the XML will be loaded into-->
</span>
XML 文件是;
<document_list>
<document>
<document_name>Holidays.pdf</document_name>
<file_type>.pdf</file_type>
<file_location></file_location>
</document>
<document>
<document_name>iPhone.jsNotes.docx</document_name>
<file_type>.docx</file_type>
<file_location></file_location>
</document>
</document_list>
在控制台中,出现第一条消息,但没有任何反应,仅此而已。但我真的(就像,真的新)对 XML 和解析,不明白什么是错的。你能帮忙吗?
【问题讨论】:
-
你尝试过使用 XMLHttpRequest 吗?
-
我在哪里说
new HttpRequest? -
好的,我试过了,现在它在
document.getElementById... 行上中断了 -
var xmlhttp = new HttpRequest(); -
是的,我在你的最后两行中发现了问题..看看我的回答,告诉我它是否有效..
标签: javascript html xml xml-parsing