【问题标题】:Unable to completely populate HTML table from XML data无法从 XML 数据完全填充 HTML 表
【发布时间】:2016-11-09 12:41:25
【问题描述】:

在这里,我尝试使用 AJAX 调用返回的 XML 数据填充 HTML 表。

我的问题是我无法完全填充 HTML 表格。因为,我对 AJAX 很陌生,所以我无法弄清楚 XML 解释是如何工作的,以及如何解码错误的根本原因。

这是我的网页代码

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>
        table, th, td 
        {
            border: 1px solid black;
            border-collapse: collapse;
        }
        th, td 
        {
            padding: 5px;
        }
    </style>
</head>
<body>
    <h1>Enter Book Details</h1><br>

    <form method="post" action="http://localhost:8080/mysql/glarimy/lib/addbook" onsubmit="myButton.disabled = true; return true;">
        Title :<br> <input type = "text" name = "title" ><br>
        Author :<br><input type = "text" name = "author" ><br>
        Price : <br><input type = "text" name = "price" ><br>
        No. of Pages :<br><input type="text" name = "pages" ><br>
        <input type = "submit" id="submit" value = "Add Book">
        <input type = "reset" value ="Reset"><br><br>
    </form>

    <button onclick = "location.href = 'http://localhost:8080/mysql/glarimy/lib/getall'" type = "button">Get all book data in XML</button>
    <button type="button" onclick="loadXMLDoc()">Get all books data </button>
    <table id="demo"></table>   
    <script>
        function loadXMLDoc() 
        {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() 
            {
                if (this.readyState == 4 && this.status == 200) 
                {
                  myFunction(this);
                }
            };
            xmlhttp.open("GET", "http://localhost:8080/mysql/glarimy/lib/getall", true);
            xmlhttp.send();
        }
        function myFunction(xml) 
        {
            var i;
            var xmlDoc = xml.responseXML;
            var table="<tr><th>ISBN</th><th>Title</th><th>Author</th><th>Price</th><th>No. of Pages</th></tr>";
            var x = xmlDoc.getElementsByTagName("book");
            for (i = 0; i < x.length; i++) 
            { 
                table += "<tr><td>" +
                x[i].getElementsByTagName("isbn")[0].childNodes[0].nodeValue +
                "</td><td>" +
                x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue +
                "</td><td>";
                x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue +
                "</td></td>";
                x[i].getElementsByTagName("pages")[0].childNodes[0].nodeValue +
                "</td><td>" +
                x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue +
                "</td><tr>";
           }
            document.getElementById("demo").innerHTML = table;
        }
    </script>   
</body>
</html>

我从 AJAX 调用中得到如下 XML 数据

<books>
     <book>     
          <author>mark</author>
          <isbn>1</isbn>
          <pages>500</pages>
          <price>2000.0</price>
          <title>java</title>
     </book>
     <book>
          <author>john</author>
          <isbn>2</isbn>
          <pages>100</pages>
          <price>1000.0</price>
          <title>advancedjava</title>
     </book>
     <book>
          <author>paul</author>
          <isbn>3</isbn>
          <pages>500</pages>
          <price>500.0</price>
          <title>tomcat</title>
     </book>
     <book>
          <author>jack</author>
          <isbn>4</isbn>
          <pages>1000</pages>
          <price>5000.0</price>
          <title>rest</title>
     </book>
</books>

我得到的输出是 Click Here

谁能解释一下 AJAX 代码是如何工作的?我无法追踪那个 XML 解码部分。

更新输出:

【问题讨论】:

  • 根据var table,将行添加到表中的 for 循环顺序不正确,因此这是表中项目顺序错误的问题
  • @KevinKloet 你能详细说明你的评论吗?我对我的问题进行了一些更改,请检查一次。
  • 即使在您的 for 循环中进行了编辑之后,添加到表中的最后 2 个 td 也被颠倒了,ISBN, Title, Author, Price, No. of Pagesvar table 中的顺序,ISBN, Title, Author, No. of Pages, Price 是for 循环。
  • @KevinKloet 请检查新图片

标签: javascript jquery ajax xml rest


【解决方案1】:

看起来问题在于您的桌子的构建。当您不需要并且有太多分号时,您正在关闭 &lt;tr&gt; 标记。这是修复:

for (i = 0; i < x.length; i++) 
{ 
    table += "<tr><td>" +
    x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("isbn")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("pages")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue +
    "</td></tr>";
}

您可以在这张图片中看到您的代码存在的问题:

【讨论】:

  • 你确定这是准确的代码吗?请注意,您应该将分号 (;) 删除...
  • 这是我犯的一个愚蠢的错误。但即使在我改变它之后,我也没有得到 5 列数据,只显示了两列。检查我在问题中所做的修改。
  • 您的问题中仍有分号(编辑后)。仔细检查。
  • 这是我在代码中使用的参考 (w3schools.com/xml/ajax_xmlfile.asp) 你指的是哪个分号?
  • 让我们从我对您的代码的回答中复制和粘贴整个 for ( ...) { ... } 循环开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2015-04-21
  • 1970-01-01
  • 2013-07-04
相关资源
最近更新 更多