求助,关于xml的问题

wudichaoren2010 wudichaoren2010 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


<?xml version="1.0" encoding="utf-8"?>
<students>
<stu>
<id>a1</id>
<name>赵</name>
<c>75</c>
</stu>

<stu>
<id>a2</id>
<name>钱</name>
<c>90</c>
</stu>

<stu>
<id>a3</id>
<name>孙</name>
<c>87</c>
</stu>

<stu>
<id>a4</id>
<name>李</name>
<c>66</c>
</stu>

<stu>
<id>a5</id>
<name>周</name>
<c>31</c>
</stu>

<stu>
<id>a6</id>
<name>吴</name>
<c>78</c>
</stu>
</students>








<!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>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>LJ读取</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() {
alert("ks");
var xmldoc = loadXMLDoc("score.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("stu");
alert(nodes[0].childNodes[0].firstChild.nodeValue);
for (var i = 0; i < nodes.length; i++) {
msg += "<tr>";

msg += "<td>" + nodes[0].childNodes[0].firstChild.nodeValue + "</td>";
//msg += "<td>" + nodes[i].getElementsByTagName("id")[0].firstChild.nodeValue + "</td>";
//msg += "<td>" + nodes[i].getElementsByTagName("name")[0].firstChild.nodeValue + "</td>";
// msg += "<td>" + nodes[i].getElementsByTagName("c")[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>






alert(nodes[0].childNodes[0].firstChild.nodeValue);为什么这句无法获取值?







// 加载xml文档
var loadXML = function (xmlFile) {
var xmlDoc;
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');//IE浏览器
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (isFirefox=navigator.userAgent.indexOf("Firefox")>0) { //火狐浏览器
//else if (document.implementation && document.implementation.createDocument) {//这里主要是对谷歌浏览器进行处理
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else{ //谷歌浏览器
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",xmlFile,false);
xmlhttp.send(null);
if(xmlhttp.readyState == 4){
xmlDoc = xmlhttp.responseXML.documentElement;
}
}
return xmlDoc;
}
// 首先对xml对象进行判断
var checkXMLDocObj = function (xmlFile) {
var xmlDoc = loadXML(xmlFile);
if (xmlDoc == null) {
alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
window.location.href = '../err.html';
}
return xmlDoc;
}




document.implementation && document.implementation.createDocument是什么意思?
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
wudichaoren2010
JavaScript 87,984 成员 224,681 内容