javascript - 使用 JavaScript 获取 XML 数据

标签 javascript xml

我感觉我要拔掉我的头发了。我正在尝试从 xml 文件中获取一些数据并使用 javascript 将其放入表中。我已经查看并遵循了大约一百万个示例,但由于某种原因,我无法让它工作。我一直在使用 DreamWeaver、Notepad++ 和 Brackets。 这是我的 html 文件。

    <head>
//<meta http-equiv="Content-Type" content="text/html; charset-UTF-8"/>
<script language="javascript" type="text/javascript" src="script2.js"></script>

<title>Computer Science Faculty Information</title>

<h1>Computer Science Faculty Information</h1>

<button onclick="readXML()">Read XML File</button>

<table id="first" border="1">
    <tr>
        <th>Name</th>
    </tr>
</table>

我的 xml 文件

<?xml version="1.0?>
<department>
<employee>
    <name>Ajay Katangur, Ph.D.</name>
    <title>Program Coordinator</title>`
    <office>CI 340</office>
    <phone>361-825-2478</phone>        
    <email>ajay.katangur@tamucc.edu</email>
</employee>
</department>

和我的js文件

function readXML() {
"use strict";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
        myFunction(xmlhttp);
    }
};
xmlhttp.open("GET", "xml.xml", true);
xmlhttp.send();

}

function myFunction(xml) {
"use strict";
var x, i, xmlDoc, table;
xmlDoc = xml.responseXML;
table = "<tr><th>Name</th><th>Title</th><th>Office</th><th>Phone</th><th>Email</th><tr>";
x = xmlDoc.getElementsByTagName("employee");
for(i = 0; i < x.length; i++) {
    table += "<tr><td>" + x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("office")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("phone")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</td></tr>";
}
document.getElementById("first").innerHTML = table;

}

值得一提的是,这里的 js 代码很大一部分来自 W3Schools。请有人告诉我我做错了什么。提前致谢。

最佳答案

您的代码的工作方式如我在下面创建的代码片段中所示。但是,正如您在评论中指出的那样,问题是您的请求没有返回 XML 文件。检查 URL 以确保其正确。

还要确保您的 XML 正确。例如,您的 XML 声明缺少引号,即应该是 <?xml version="1.0"?>并确保文件中该声明之前没有空格。

运行该代码片段以查看其工作情况。您可以在文本框中编辑 XML,然后单击按钮查看更改。

function readXML() {

  "use strict";
  
  
  // simulated request
  var xmlhttp={};
  xmlhttp.responseText = window.data.value; 
  xmlhttp.responseXML = (new DOMParser()).parseFromString(xmlhttp.responseText, 'text/xml')
  myFunction(xmlhttp);


/*  
  var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
          myFunction(xmlhttp);
      }
    };
    xmlhttp.open("GET", "xml.xml", true);
    xmlhttp.send();
  }
*/

}


function myFunction(xml) {
  "use strict";
  var x, i, xmlDoc, table;
  xmlDoc = xml.responseXML;
  table = "<tr><th>Name</th><th>Title</th><th>Office</th><th>Phone</th><th>Email</th><tr>";
  x = xmlDoc.getElementsByTagName("employee");
  for (i = 0; i < x.length; i++) {
    table += "<tr><td>" + x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("office")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("phone")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</td></tr>";
  }
  document.getElementById("first").innerHTML = table;

}
textarea {
  width: 90%;
  height: 40em;
  padding: 0.5em;
  border: 1px solid black;
  background-color: aliceblue;
}

table {
  border-collapse: collapse;
}
td {
  border: 1px lightgray solid;
  padding: 2px;
}
<button onclick="readXML()">Read XML File</button>

<table id="first" border="1">
  <tr>
    <th>Name</th>
  </tr>
</table>


<h4>XML:</h4>
<textarea id="data">
<?xml version="1.0" encoding="UTF-8" ?>
    <department>
      <employee>
        <name>John Smith, Ph.D.</name>
        <title>Program Coordinator</title>`
        <office>CI 340</office>
        <phone>000-000-0000</phone>
        <email>John.Smith@tamucc.edu</email>
      </employee>
    </department>
  </textarea>

关于javascript - 使用 JavaScript 获取 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35686672/

相关文章:

javascript - 混淆了js中的变量

javascript - 访问 RedQueryBuilder 中的请求结构

java - 解析连续的 XML 文档流

javascript - 如何单击自动构建的 li 列表的功能

javascript - JSON 请求的无效标签错误

javascript - 扩展应用程序 : Uncaught TypeError: undefined is not a function

c# - 在 ASP.NET MVC 中编写 XML 文件?

java - 比较具有多个属性的 bean,每个属性取决于两个 bean

javascript - 从浏览器启动 exe (Windows)

java - xpath 在一次调用中解析多个值