0

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 Uncaught TypeError: Cannot read property 'data' of null 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.

So far, I am convinced that XML is evil.

Sample of XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<mediaGroup>
  <amgVersion version="1.3" />
  <groupInfo>
    <bunch of="data I don't care about" />
  </groupInfo>
  <sizes>
    <bunch of="data I don't care about" />
  </sizes>

  <media totalGallerySize="104">
    <item>
      <title></title>
      <description>Yo! Look at that monkey on the bars</description>
      <imageID />
      <mediaType/>
      <renditions>
        <rendition size="large" />
        <rendition size="medium" />
        <rendition size="small" />
        <rendition size="thumb"  />
      </renditions>
    </item>
    <item>
      <title></title>
      <description>It's trying to parse XML files ahhaha</description>
      <imageID />
      <mediaType/>
      <renditions>
        <rendition size="large" />
        <rendition size="medium" />
        <rendition size="small" />
        <rendition size="thumb"  />
      </renditions>
    </item>
    <item>
      <title></title>
      <description></description>
      <imageID />
      <mediaType/>
      <renditions>
        <rendition size="large" />
        <rendition size="medium" />
        <rendition size="small" />
        <rendition size="thumb"  />
      </renditions>
    </item>
    <item>
      <title></title>
      <description>That missing description is going to make him angry</description>
      <imageID />
      <mediaType/>
      <renditions>
        <rendition size="large" />
        <rendition size="medium" />
        <rendition size="small" />
        <rendition size="thumb"  />
      </renditions>
    </item>  
  </mediaGroup>

For my code I have the following:

<script type="text/javascript">
  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<=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 + "<br />");
    }
  }
 </script>

Line 38: Uncaught TypeError: Cannot read property 'data' of null Line 38 = if (typeof nameELEM.firstChild.data !== "undefined") {

Mallow
  • 844
  • 1
  • 13
  • 37
  • The issue may be how you're checking for null. See: http://stackoverflow.com/questions/776950/javascript-undefined-undefined – Diodeus - James MacFarlane Jan 09 '12 at 21:41
  • @Diodeus I'm afriad I don't understand :-( – Mallow Jan 09 '12 at 21:51
  • Please elaborate on "just doesn't work" and "the code insists...". What actual error messages (or other unexpected objective behaviors) are you seeing? Also, I can't make sense of "null in between the tags". Tags are an aspect of serialization. null is an object in memory. What are you trying to describe? – LarsH Jan 09 '12 at 21:53
  • @LarsH Hi, thanks for the comment, I've attempted to clarify my question above and remove all that fluff. The error message I am getting is Uncaught TypeError: Cannot read property 'data' of null, and the code I have will display the information I want but it's execution halts when it meets an empty tag. "describe" is just the name of the tags such as, Stuff in here – Mallow Jan 10 '12 at 14:38
  • P.S. So hopefully you're no longer convinced that XML is evil... it's just cantankerous. Rub it the right way, and it will purr like a kitten. Hopefully it won't also eat your houseplants and regurgitate on the carpet. – LarsH Jan 10 '12 at 17:15
  • It's a little less evil. It's about time I get into some XML, I have a feeling it will make many projects a whole lot easier to organize. Are there any good links on how to structure XML data. I have some tabular data, (a schedule of events) to program off of two days, I was thinking something like >> >...> ...>, The only thing is I don't know if I am being redundant, and not sure I am choosing the right way. A sql database seems a little overkill for something so simple. – Mallow Jan 10 '12 at 17:24

1 Answers1

2

Instead of:

if (nameELEM.firstChild.data !== null)

Try:

if (typeof nameELEM.firstChild.data !== "undefined")

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • I've changed it and tried it in the code above but I am still getting the same error :-( – Mallow Jan 10 '12 at 14:38
  • alert(typeof nameELEM.firstChild.data) -- see what you get. – Diodeus - James MacFarlane Jan 10 '12 at 14:41
  • That's weird the execution just stops... I did alert(i + typeof nameELEM.firstChild.data) and my result was as follows: 1string, 2string and it stopped there, it didn't give me the third or fourth one. – Mallow Jan 10 '12 at 15:05
  • 1
    @Mallow, maybe you need to check `nameELEM.firstChild` instead of `nameELEM.firstChild.data`. If the element has no children, firstChild won't exist. – LarsH Jan 10 '12 at 15:24
  • 1
    @LarsH Ha-zaaa!! Hip-hip Horray!! Thank you for the XML lessons, It worked, and it makes sense now. I realized that testing typeof nameElem.firstChild.data or .firstChild against null was like saying 'object'!==null or 'string'!==null... My initial mistake going into this was trying to do .data!==null. I am happy it works, and I got a good lesson out of it, thank you very much ^_^. – Mallow Jan 10 '12 at 15:54
  • @Mallow, :-) Glad your problem was solved. A high-5 to Diodeus for providing a robust way to check for an undefined value in JS. – LarsH Jan 10 '12 at 17:05