Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issue parsing XML file into table

    I'm trying to use a script to parse an xml file to build a table that is set to launch a new window when the row is clicked on. The new window should show more details from the original xml file. I'm getting an error "Uncaught TypeError: Cannot read property 'length' of null". As far as I can see, it's picking up the xml file properly but I can't figure out where this error is coming from. The script I'm using is below, can anyone tell me what I'm doing wrong?


    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>"Wine list and review loader."</title>
    <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","wineList.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    wineFile=xmlDoc.getElementById("WINE");
    
    function showReview(i)
    {
    var w=window.open();
    w.document.open();
    wine=(wineFile[i].getElementsByTagName("WINENAME")[0].childNodes[0].nodeValue);
    winery=(wineFile[i].getElementsByTagName("WINERYNAME")[0].childNodes[0].nodeValue);
    review=(wineFile[i].getElementsByTagName("REVIEW")[0].childNodes[0].nodeValue);
    txt="Wine: "+wine+"<br>Winery: "+wineryname+"<br>Review: "+review+"<br>;
    w.document.write(document.getElementById("displayReview").innerHTML=txt);
    w.document.open();
    }
    </script>
    </head>
    
    <div id="displayReview">Click on a wine to see the review.</div>
    <br>
    <body>
    <script>
    document.write("<table><tr><th>Wine</th><th>Winery</th></tr>");
    for (var i=0;i<wineFile.length;i++)
      { 
      document.write("<tr onclick=showReview("+i+")>");
      document.write("<td>");
      document.write(wineFile[i].getElementsByTagName("WINENAME")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(wineFile[i].getElementsByTagName("WINERYNAME")[0].childNodes[0].nodeValue);
      document.write("</td></tr>");
      }
    document.write("</table>");
    </script>
    </body>
    </html>
    Last edited by vinyl-junkie; 07-24-2013 at 01:31 PM. Reason: added code tags

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    When you document.write, it wipes out the entire page and outputs the specified text. So your first document.write causes the wineFile var to be null. Use innerHTML or DOM methods instead.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •