1 Reply Latest reply on Oct 31, 2008 9:50 AM by Newsgroup_User

    Parsing Through XML with Actionscript

    Galwegian Level 1
      I have an xml file at the moment that has a form something like . . .

      <application>
      <app1 name="App1">
      <module id="1"/>
      <module id="2"/>
      </app1>
      <app2 name="App2">
      <module id="3"/>
      </app2>
      </application>

      What I went to do is to read this file in a breadth-first type of fashion. In other words, when I parse the file I read the elements in the following order, as opposed to just read them one after the other.

      Application
      App1
      App2
      Module id="1"
      Module id="2"
      Module id="3"

      Has anyone got an example of a peice of code that will do that for me? I'm sure I could use recursion, but I've forgotten all that stuff since college :P
        • 1. Re: Parsing Through XML with Actionscript
          Level 7

          "Galwegian" <webforumsuser@macromedia.com> wrote in message
          news:gef942$sql$1@forums.macromedia.com...
          >I have an xml file at the moment that has a form something like . . .
          >
          > <application>
          > <app1 name="App1">
          > <module id="1"/>
          > <module id="2"/>
          > </app1>
          > <app2 name="App2">
          > <module id="3"/>
          > </app2>
          > </application>
          >
          > What I went to do is to read this file in a breadth-first type of fashion.
          > In
          > other words, when I parse the file I read the elements in the following
          > order,
          > as opposed to just read them one after the other.
          >
          > Application
          > App1
          > App2
          > Module id="1"
          > Module id="2"
          > Module id="3"
          >
          > Has anyone got an example of a peice of code that will do that for me? I'm
          > sure I could use recursion, but I've forgotten all that stuff since
          > college :P

          Say you have your XML in a variable, myXML:

          var:widthPrint:String =printAll(new XMLList(myXML));

          private function printAll(theXML:XMLList):String {
          var result:String;
          var xmlLength:int = theXML.length();
          for (var i:int=0; i<xmlLength; i++) {
          var child:XML = theXML.child(i);
          //store the name of the node
          result = child.name() + ' ';
          var atts:XMLList = child.attributes();
          var numAttributes:int = atts.length();
          for (var j:int; j<numAttributes; j++) {
          var att:XML= child.atts[j];
          //store the name and value of each attribute
          result += att.name() + "=" + att.text() + " ";
          }
          //setup for next level
          var children:XMLList = theXML.children();
          if (children.length()>0) {
          result += "\n" + printAll(children);
          }
          return result;
          }
          }

          Note this is off the top of my head, but it should get you close. Also note
          that tis will print out
          application
          app1 name=App1
          ...

          For reasons that should be obvious. If you want it to do something
          different, you need to put in checking in there to "promote" the name
          attribute and suppress the actual name when that's what you want to print
          out. Or you can just change the XML to more closely reflect what you want
          to see.

          HTH;

          Amy