Unable to get property of GetElementsbyTagName of undefined or null reference

user3147955 picture user3147955 · Dec 31, 2013 · Viewed 9.1k times · Source

Let me start by stating I am a Newb and while I have created a fair bit of html code, I am trying to move into XML and Javascipt for my own edification. That being said, I have run into a problem (which I researched and found similar issues) that has stumped me. The funny thing the code worked a few times under Firefox but now it works on nothing and I can't seem to find the issue. I did use the IE Console to check file open status etc.. but no joy. As the subject line indicates I can't seem to list the XML tag(s) in a table I am building for a web page. Here is the relevant html code and associated XML file. Any assistance would be appreciated.

HTML Code

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

Answer

Kenney picture Kenney · Jan 1, 2014

getElementsByTagName returns a NodeList, not an array, so use x.item(i).

To get it to work, make the following changes:

1) change true to false for synchronous (line 11)

xmlhttp.open("GET","event.xml",false);

2) declare the month variable (line 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) use .item(i) and .item(0) rather than [i] and [0] (line 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);