Позвольте мне начать с того, что я - Newb, и пока я создал справедливый бит 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>
getElementsByTagName
возвращает NodeList, а не массив, поэтому используйте x.item(i)
.
Чтобы заставить его работать, внесите следующие изменения:
1) измените значение true на false для синхронного (строка 11)
xmlhttp.open("GET","event.xml",false);
2) объявить переменную month
(строка 31+)
for (i=0;i<y.length;i++)
{
var month = y.item(i); // insert this line
document.write("<tr bgcolor='#6CA6CD'>");
document.write("<th>");
document.write(month.getElementsByTagName("month1").item(0).childNodes[0].nodeValue); // !!!
3) используйте .item(i)
и .item(0)
а не [i]
и [0]
(строка 45+)
var x=xmlDoc.getElementsByTagName("month_event");
for (i=0;i<x.length;i++)
{
document.write("<tr><td bgcolor='white'>");
document.write("<b>test ");
document.write(x.item(i).getElementsByTagName("month1_date").item(0).childNodes[0].nodeValue);
xmlhttp.responseXML
доxmlhttp.responseXML
пор, пока он не загрузится.onreadystatechange
событияonreadystatechange
, иначе ответ не будет готов, пока вы не используете синхронный режим.