title: Javascript - Unable to get data from a nested XML file - Cannot read property of null
body: <p>I exported a flash web gallery from Lightroom. It uses an XML file to display the caption of each photo. All I want is to extract the description tag from this XML file so that I can send it to a proofreader. I've been trying to use javascript to do this but it just isn't working. The code I have is able to get me a partial list of all the descriptions. My code iterates through the description tags but once it meets an empty tag:  it (using Chrome debugger) throws a <strong>Uncaught TypeError: Cannot read property 'data' of null</strong> and stops execution. 
  I've tried to check against "null !==" but the I am guessing the code still runs because I still get the error. </p>
  
  <p>So far, I am convinced that XML is evil.</p>
  
  <p>Sample of XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;mediaGroup&gt;
    &lt;amgVersion version="1.3" /&gt;
    &lt;groupInfo&gt;
      &lt;bunch of="data I don't care about" /&gt;
    &lt;/groupInfo&gt;
    &lt;sizes&gt;
      &lt;bunch of="data I don't care about" /&gt;
    &lt;/sizes&gt;
  
    &lt;media totalGallerySize="104"&gt;
      &lt;item&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;description&gt;Yo! Look at that monkey on the bars&lt;/description&gt;
        &lt;imageID /&gt;
        &lt;mediaType/&gt;
        &lt;renditions&gt;
          &lt;rendition size="large" /&gt;
          &lt;rendition size="medium" /&gt;
          &lt;rendition size="small" /&gt;
          &lt;rendition size="thumb"  /&gt;
        &lt;/renditions&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;description&gt;It's trying to parse XML files ahhaha&lt;/description&gt;
        &lt;imageID /&gt;
        &lt;mediaType/&gt;
        &lt;renditions&gt;
          &lt;rendition size="large" /&gt;
          &lt;rendition size="medium" /&gt;
          &lt;rendition size="small" /&gt;
          &lt;rendition size="thumb"  /&gt;
        &lt;/renditions&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;description&gt;&lt;/description&gt;
        &lt;imageID /&gt;
        &lt;mediaType/&gt;
        &lt;renditions&gt;
          &lt;rendition size="large" /&gt;
          &lt;rendition size="medium" /&gt;
          &lt;rendition size="small" /&gt;
          &lt;rendition size="thumb"  /&gt;
        &lt;/renditions&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;description&gt;That missing description is going to make him angry&lt;/description&gt;
        &lt;imageID /&gt;
        &lt;mediaType/&gt;
        &lt;renditions&gt;
          &lt;rendition size="large" /&gt;
          &lt;rendition size="medium" /&gt;
          &lt;rendition size="small" /&gt;
          &lt;rendition size="thumb"  /&gt;
        &lt;/renditions&gt;
      &lt;/item&gt;  
    &lt;/mediaGroup&gt;
  </code></pre>
  
  <p>For my code I have the following: </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    function loadXMLDoc(dname) {
  if (window.XMLHttpRequest)
      {
      xhttp=new XMLHttpRequest();
      }
  else
      {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
  xhttp.open("GET",dname,false);
  xhttp.send();
  return xhttp.responseXML;
    } //end function
  
    xmlDoc=loadXMLDoc("group.xml");
    var z=xmlDoc.getElementsByTagName("item"), nameELEM;
    document.write(z.length);
    for (i=0;i&lt;=z.length;i++){
      nameELEM=z[i].getElementsByTagName("description")[0];
      if (typeof nameELEM.firstChild.data !== "undefined") { //This is the line the error quotes
        //handle property xxx of documentFragment as required
        document.write(nameELEM.firstChild.data + "&lt;br /&gt;");
      }
    }
   &lt;/script&gt;
  </code></pre>
  
  <p>Line 38: Uncaught TypeError: Cannot read property 'data' of null
  Line 38 = if (typeof nameELEM.firstChild.data !== "undefined") { </p>