2 Replies Latest reply on Apr 9, 2009 11:42 AM by kevsm

    Storing XML data with JavaScript

      I've been playing around with the following code, trying to pull data from an XML file and write it into a table. So far I can't figure out how to store it into a global variable, as it always returns a null value when I try and print it out onto the screen. I'm also unable to do a document.write to print it out, the only thing that seems to work is creating an element and appending it to the body. Am I missing something big here? Thanks in advanced.

       

      var contacts = air.File.applicationDirectory.resolvePath
      ( 'data.xml' );
      var elem = null;
      var first = null;
      var last = null;
      var rolodex = null;

      function doLoad() {
      var xml = new XMLHttpRequest();
      xml.onreadystatechange = function()
      {
      if( xml.readyState == 4 )
      {
      rolodex = xml.responseXML.documentElement.
      getElementsByTagName
      ( 'status' );
      for( var c = 0; c < rolodex.length; c++ )
      {
      first = rolodex[c].getElementsByTagName
      ( 'text' )[0].textContent;
      last = rolodex[c].getElementsByTagName
      ( 'id' )[0].textContent;
      elem = document.createElement("div");
      elem.innerText = first + " " + last;
      document.body.appendChild(elem);
      alert(rolodex);
      }
      }
      }
      xml.open( 'GET', contacts.url, true );
      xml.send( null );
      }

        • 1. Re: Storing XML data with JavaScript
          Joe ... Ward Level 4

          Are you saying that alert(rolodex) claims that rolodex is null, even though you have just successfully looped through its contents?

           

          document.write() will only work while document is loading. Calling it after the document is loaded would normally create a new document, replacing the first -- however, this is not allowed in the AIR application sandbox, so document.write does nothing in this case.

          • 2. Re: Storing XML data with JavaScript
            Level 1

            That's actually supposed to be alert(first); which works fine and shows me the contents, but when I do a document.write, it comes back as null. But that makes sense now with your explanation.