JS读取XML并以表格展示:兼容IE与Firefox的实现方案
本文介绍一种使用JavaScript读取XML文件数据并以表格形式展示的方法。重点解决了Firefox浏览器中childNodes获取节点时可能返回换行符的问题,从而实现了在IE和Firefox等浏览器中的兼容显示。
JS读取XML并以表格展示:兼容IE与Firefox的实现方案
本文介绍了一种通过JavaScript读取XML文件数据,并将其以表格形式展示的方法。该方案旨在提供实用的参考示例。
首先,我们需要准备一个XML数据文件。
<?xml version="1.0" standalone="yes"?>
<student>
<stuinfo>
<stuName>张秋丽</stuName>
<stuSex>女 </stuSex>
<stuAge>18</stuAge>
</stuinfo>
<stuinfo>
<stuName>李文才</stuName>
<stuSex>男 </stuSex>
<stuAge>31</stuAge>
</stuinfo>
<stuinfo>
<stuName>李斯文</stuName>
<stuSex>男 </stuSex>
<stuAge>22</stuAge>
</stuinfo>
<stuinfo>
<stuName>马英</stuName>
<stuSex>女 </stuSex>
<stuAge>25</stuAge>
</stuinfo>
<stuinfo>
<stuName>孙红雷</stuName>
<stuSex>男 </stuSex>
<stuAge>32</stuAge>
</stuinfo>
<stuinfo>
<stuName>欧阳俊雄</stuName>
<stuSex>男 </stuSex>
<stuAge>28</stuAge>
</stuinfo>
<stuinfo>
<stuName>江琳</stuName>
<stuSex>女 </stuSex>
<stuAge>23</stuAge>
</stuinfo>
<stuinfo>
<stuName>小小</stuName>
<stuSex>女 </stuSex>
<stuAge>22</stuAge>
</stuinfo>
</student>
接下来是ASPX页面的具体代码实现。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="获取数据库数据生成XML.aspx.cs" Inherits="Chapter1.获取数据库数据生成XML" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", dname, false);
xhttp.send("");
return xhttp.responseXML;
}
function ReadXml() {
var xmldoc = loadXMLDoc("Student.xml");
//获得指定节点
var divmsg = document.getElementById("xmlMsg");
var msg = "<table border='1' id='mytable'><tr><th>姓名</th><th>性别</th><th>年龄</th><tr>";
var nodes = xmldoc.getElementsByTagName("stuinfo");
for (var i = 0; i < nodes.length; i++) {
msg += "<tr>";
msg += "<td>" + nodes[i].getElementsByTagName("stuName")[0].firstChild.nodeValue + "</td>";
msg += "<td>" + nodes[i].getElementsByTagName("stuSex")[0].firstChild.nodeValue + "</td>";
msg += "<td>" + nodes[i].getElementsByTagName("stuAge")[0].firstChild.nodeValue + "</td>";
msg += "</tr>";
}
msg += "</table>";
divmsg.innerHTML = msg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="JS读取XML" onclick="ReadXml()" /><br />
<div id="xmlMsg">
</div>
</div>
</form>
</body>
</html>
上述JavaScript代码的关键在于避免直接使用childNodes属性。因为在Firefox浏览器中,childNodes[0]有时会获取到换行符"\n",而非预期的第一个子节点。通过这种处理方式,可以有效解决兼容性问题,确保在IE和Firefox浏览器中正常运行(其他浏览器未进行广泛测试)。
如需了解更多JavaScript相关知识,可参考本站系列教程,包括:《JavaScript操作XML文件技巧总结》、《JavaScript中ajax操作技巧总结》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》。
希望本文提供的解决方案能对JavaScript程序设计有所帮助。
D3.js 比例尺与坐标轴的配置详解
2026-11-05 16:54:46
JavaScript怎么遍历对象的Symbol键和不可枚举属性
2026-11-05 13:13:39
封装一个健壮的JS字符串判空工具函数
2026-11-05 12:16:14
JavaScript合并数组的三种常用方法:concat、扩展运算符与push
2026-11-05 11:22:29
JavaScript立即执行函数(IIFE)是什么及基本语法
2026-11-05 10:51:19
