html - 如何使用 FireFox 在 html 中显示 xml?
问题描述
我必须在 HTML 文件的表格中显示 XML 数据。
我尝试关闭 FireFox 上的所有扩展程序,但仍然出现空白屏幕。
员工详情
<script>
var client;
if (window.XMLHttpRequest) {
client = new XMLHttpRequest();
}
else {
client = new ActiveXObject("Microsoft.XMLHTTP");
}
client.open('GET', 'employees.xml', false);
client.onreadystatechange = function() { // will run when the file loads
// get the response XML
var xmlDoc = client.responseXML;
// get the list of user
var user = xmlDoc.getElementsByTagName("user");
// get the container where you want to embed the table
var container = document.getElementById("container");
var tableString = "<table border='1'>"; // Make a table and put the element data inside it
for (i = 0; i < user.length; i++) {
tableString += "<tr><td>";
tableString += user[i].getElementsByTagName("Firstname")[0].childNodes[0].nodeValue;
tableString +="</td><td>";
tableString += user[i].getElementsByTagName("Phone")[0].childNodes[0].nodeValue;
tableString += "</td></tr>";
}
tableString += "</table>";
// append the table to the container
container.innerHTML = tableString;
}
client.send();
</script>
<body>
<h1></h1>
<div id="container"></div>
</body>
<?xml version = "1.0" encoding ="UTF-16"?>
<user_information>
<user id="1">
<Firstname> Bobby </Firstname>
<Lastname>Drake</Lastname>
<major>Criminal Justice</major>
<location>Illinois</location>
<Phone>(923)949-2302</Phone>
</user>
<user>
<Firstname>Scott</Firstname>
<Lastname>Summers</Lastname>
<major> </major>
<location> California </location>
<Phone>(395)984-7284</Phone>
</user>
<user>
<Firstname>Jean</Firstname>
<Lastname>Grey</Lastname>
<major> </major>
<location>New York</location>
<Phone>(843)759-6943</Phone>
</user>
<user>
<Firstname>Kevin</Firstname>
<Lastname>Sydney</Lastname>
<major> </major>
<location>New York</location>
<Phone>(571)-089-4568</Phone>
</user>
<user>
<Firstname>Suzanne</Firstname>
<Lastname>Chan</Lastname>
<major> </major>
<location>Illinois</location>
<Phone>(685)-583-2849</Phone>
</user>
</user_information>
在 FireFox 中打开 html 时出现空白屏幕。我不知道这是我的代码还是我电脑上的东西。如果有人可以帮助我,我将不胜感激。
解决方案
我假设您正在尝试使用 file:/// 或从导致 CORS 策略违规的本地目录访问这些文件,并且您的请求被阻止。
您可以使用网络服务器来部署资源并访问它。或者查看相关的线程评论,它可以帮助您使用预期的 CORS 策略运行 Chrome。
推荐阅读
- arrays - 过滤位于 pyspark.sql.dataframe 单元格中的数组
- javascript - 打字稿将字符串解析为 345-67 的数字组和包含单词和数字的文本
- r - R用一个值/字符串连接数据框中的列
- angular - 角材料表宽度 100%
- c# - 如何将重点放在流程上?
- ios - swift tableView 为零
- c++ - 带有指向 STL 容器的迭代器的模板类
- javascript - 是否有一种优雅的方式将 npm 运行选项传递给 grunt 选项
- highcharts - Highcharts 奇怪的线条
- c# - Xamarin Android 不加载 AndroidManifest.xml