0

我编写了以下代码来读取XML文件并显示它,但它不起作用。

谁能告诉我哪里出错了?尝试加载并显示 XML:

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "books.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

var tbl = document.createElement("table");
tbl.className = "datatable";
tbl.border = "6";
tbl.width = "500";
tbl.cellPadding = "10";

var tbody = document.createElement("tbody");

var x = xmlDoc.getElementsByTagName("Book");

for (i = 0; i < x.length; i++) {

    var title = x[i].childNodes[0].text;
    var author = x[i].childNodes[1].text;

    //var title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    //var author = x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;

    var row = document.createElement("tr");

    var td = document.createElement("td");
    td.appendChild(title);

    //td.appendChild(author);

    row.appendChild(td);

}
tbody.appendChild(row);
tbl.appendChild(tbody);
document.getElementById("content").appendChild(tbl);
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
    <title>A Bend in the river</title>
    <author>V.S. Naipaul</author>
</Book>
<Book>
    <title>Earth</title>
    <author>Emile Zola</author>
</Book>
<Book>
    <title>I am not an Island</title>
    <author>V.S. Naipaul</author>
</Book>
<Book>
    <title>Macbeth</title>
    <author>William Shakespeare</author>
</Book>
<Book>
    <title>Quarantene</title>
    <author>Jim Crass</author>
</Book>
</Books>

感谢任何能提供帮助的人。

4

2 回答 2

0

尝试

  <body onload="load()">
    <div id="content"></div>
  </body>

function load() {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "books.xml", false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

    var tbl = document.createElement("table");
    tbl.className = "datatable";
    tbl.border = "6";
    tbl.width = "500";
    tbl.cellPadding = "10";

    var tbody = document.createElement("tbody");

    var x = xmlDoc.getElementsByTagName("Book");

    for (i = 0; i < x.length; i++) {
        var title = x[i].getElementsByTagName('title')[0].firstChild.nodeValue;
        var author = x[i].getElementsByTagName('author')[0].firstChild.nodeValue;

        var row = document.createElement("tr");

        var td = document.createElement("td");
        td.innerHTML = title;
        row.appendChild(td);

        td = document.createElement("td");
        td.innerHTML = author;
        row.appendChild(td);

        tbody.appendChild(row);

    }
    tbl.appendChild(tbody);
    document.getElementById("content").appendChild(tbl);

}

演示:小提琴

于 2013-09-05T03:37:36.350 回答
0

为了繁荣;整理一个demo来帮助调试(使用DOMParser跳过XMLHttpRequest;如果成功,将登录控制台<table>

演示小提琴HERE(并在页面上打开您的控制台)

就目前而言,您似乎收到了以下错误报告

Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist.

发生在哪一行

td.appendChild(title);

这很可能是由于试图将 Node 附加不是该NodeownerDocument的#document。您将需要使用来解决此问题,或手动在正确的#document中创建等效节点。document.importNode

title = document.importNode(title);
td.appendChild(title);

这也需要对所有其他Nodes进行。

于 2013-09-05T03:46:33.867 回答