我正在尝试通过所有标签的 id 解析 xml 文件。这是我的xml
<?xml version="1.0" encoding="UTF-8"?>
<zones>
<bedroom id="0">
<fan id="00">on</fan>
<tv id="01">off</tv>
</bedroom>
<hall id="1">
<tube id="10">on</tube>
<bulb id="11">off</bulb>
<fan id="12">on</fan>
<tv id="13">on</tv>
<ac id="14">on</ac>
</hall>
<kitchen id="2">
<freez id="20">on</freez>
</kitchen>
<toilet id="3">
<bulb id="30">on</bulb>
</toilet>
<bathroom id="4">
<heater id="40">on</heater>
</bathroom>
</zones>
这是我的代码..
<html>
<head> <meta http-equiv="refresh" content="1" > </head>
<body>
<script type="text/javascript" for="window" event="onload">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.open("GET","home.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementById("0");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write("fan");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("fan")[0].childNodes[0].nodeValue);
document.write("<tr><td>");
document.write("tv");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("tv")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
var x=xmlDoc.getElementsByTagName("hall");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write("tube");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("tube") [0].childNodes[0].nodeValue);
document.write("<tr><td>");
document.write("bulb");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("bulb") [0].childNodes[0].nodeValue);
document.write("<tr><td>");
document.write("fan");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("fan")[0].childNodes[0].nodeValue);
document.write("<tr><td>");
document.write("tv");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("tv")[0].childNodes[0].nodeValue);
document.write("<tr><td>");
document.write("ac");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("ac")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
var x=xmlDoc.getElementsByTagName("kitchen");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write("freez");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("freez") [0].childNodes[0].nodeValue);
}
var x=xmlDoc.getElementsByTagName("toilet");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write("bulb");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("bulb") [0].childNodes[0].nodeValue);
}
var x=xmlDoc.getElementsByTagName("bathroom");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write("heater");
document.write("</td><td>");
document.write(x[i].getElementsByTagName("heater") [0].childNodes[0].nodeValue);
}
document.write("</table>");
</script>
</body>
</html>
问题是当我用
var x=xmlDoc.getElementByTagName("bedroom");
一切正常。但是当我把它改成
var x=xmlDoc.getElementById("0");
浏览器中没有显示任何内容。所以请帮我解决这个问题,我坚持这一点。