0

私は初心者であり、かなりの量の HTML コードを作成しましたが、自分自身の教育のために XML と Javascipt に移行しようとしています。そうは言っても、私は困惑した問題(私が調査して同様の問題を見つけた)に遭遇しました。おもしろいことに、コードは Firefox で数回は機能しましたが、今では何も機能せず、問題を見つけることができないようです。IEコンソールを使用してファイルのオープンステータスなどを確認しましたが、喜びはありませんでした。件名が示すように、Web ページ用に作成しているテーブルに 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>
4

1 に答える 1

2

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)and.item(0)ではなく[i]and を使用[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);
于 2014-01-01T13:07:17.290 に答える