让我首先说明我是一个新手,虽然我已经创建了相当多的 html 代码,但我正在尝试使用 XML 和 Javascipt 来进行自己的教育。话虽如此,我遇到了一个困扰我的问题(我研究并发现了类似的问题)。有趣的是,代码在 Firefox 下运行了几次,但现在它什么也没用,我似乎找不到问题所在。我确实使用 IE 控制台来检查文件打开状态等。但没有乐趣。正如主题行所示,我似乎无法在为网页构建的表中列出 XML 标记。这是相关的 html 代码和相关的 XML 文件。任何援助将不胜感激。
HTML 代码
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","event.xml",true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState!=4)return;
if(xmlhttp.status==200)
alert(xmlhttp.responseText);
else
alert("Request failed!");
};
//onreadystatechange
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var y=xmlDoc.getElementsByTagName("months");
for (i=0;i<y.length;i++)
{
document.write("<tr bgcolor='#6CA6CD'>");
document.write("<th>");
document.write(month[i].getElementsByTagName("month1")[0].childNodes[0].nodeValue);
document.write("</th>");
document.write("<th>");
document.write(month[i].getElementsByTagName("month2")[0].childNodes[0].nodeValue);
document.write("</th>");
document.write("</tr>");
}
var x=xmlDoc.getElementsByTagName("month_event");
for (i=0;i<x.length;i++)
{
document.write("<tr><td bgcolor='white'>");
document.write("<b> ");
document.write(x[i].getElementsByTagName("month1_date")[0].childNodes[0].nodeValue);
document.write("</b> - ");
document.write(x[i].getElementsByTagName("month1_day")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].getElementsByTagName("month1_time")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].getElementsByTagName("month1_evdescription")[0].childNodes[0].nodeValue);
document.write("</td><td bgcolor='lightgrey'>");
document.write("<b> ");
document.write(x[i].getElementsByTagName("month2_date")[0].childNodes[0].nodeValue);
document.write("</b> - ");
document.write(x[i].getElementsByTagName("month2_day")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].getElementsByTagName("month2_time")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].getElementsByTagName("month2_evdescription")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<events>
<months>
<month1>December</month1>
<month2>January</month2>
</months>
<month_event>
<month1_date>22 Dec</month1_date>
<month1_day>Sunday</month1_day>
<month1_time>10:30AM - 11:00AM</month1_time>
<month1_evdescription>The Centre is closed</month1_evdescription>
<month2_date>6 Jan</month2_date>
<month2_day>Tuesday</month2_day>
<month2_time>All Day</month2_time>
<month2_evdescription>The Heavy</month2_evdescription>
</month_event>
<month_event>
<month1_date>25 Dec</month1_date>
<month1_day>Wednesday</month1_day>
<month1_time>All Day</month1_time>
<month1_evdescription>Christmas</month1_evdescription>
<month2_date>10 Jan</month2_date>
<month2_day>Tuesday</month2_day>
<month2_time>10:30AM - 11:30AM</month2_time>
<month2_evdescription>Nothing</month2_evdescription>
</month_event>
</events>