3 Replies Latest reply on Nov 4, 2010 7:16 AM by kglad

    Traversing XML file to an array?

    ChrisH

      Hi all,

      I'm having trouble interrperting how I should traverse an XML file and pushing it to an array to display the sets of data.

       

      Here is my AS:

      myXML = new XML();
      myXML.ignoreWhite = true;
      myXML.onLoad = function(ok) {

      if (ok) {
        alldata = this.firstChild.childNodes;
        allEntries = this.firstChild.firstChild.childNodes;

       

        for (i=0; i<alldata.length; i++) {
         trace(newline+"Letter: "+alldata[i].nodeName);

       

         for (r=0; r<allEntries.length; r++) {
          trace(allEntries[r].nodeName+":"+allEntries[r].firstChild);
         }

        }

       

      } else {
        trace("error loading");
      }
      };
      myXML.load("temp_glossary.xml");

       

       

      Here is my XML:

      <?xml version="1.0" encoding="iso-8859-1"?><!--This-->
      <glossary>

      <A>

      <Aardvark>This is an Aardvark</Aardvark><!--firstChild.firstChild.firstChild-->
      <Apple>This is an Apple</Apple>
      <Ape>This is an Ape</Ape>
      </A>

      <B>
      <Ball>This is a Ball</Ball>
      <Bat>This is a Bat</Bat>
      <Bike>This is a Bike</Bike>
      </B>

      <C>
      <Car>This is a Car</Car>
      <Cap>This is a Cap</Cap>
      <Cook>This is an a Cook</Cook>
      </C>
      </glossary>

       

       

      Output currently is:

      Letter: A
      Aardvark:This is an Aardvark
      Apple:This is an Apple
      Ape:This is an Ape

       

      Letter: B
      Aardvark:This is an Aardvark
      Apple:This is an Apple
      Ape:This is an Ape

       

      Letter: C
      Aardvark:This is an Aardvark
      Apple:This is an Apple
      Ape:This is an Ape

       

       

      From the output I can see that I am only access the firstChild each time I write the terms out, but I'm not sure how to go about incrementing this to the next block of terms.  I'm assuming I can do it with a 2D Array and iteration, but not sure how to implement.

      Any help would be greatly appreciated, thank you.

       

      Chris