3 Replies Latest reply on Jul 21, 2009 5:46 AM by thrill-house

    Problem with accessing XML in ActionScript 3.0

    thrill-house

      Hi,

       

      i have a problem with ActionScript 3.0. After loading an XML file via URLLoader/URLRequest into a variable (type XML), there are problems by accessing its elements. I tryed the dot-Operator(.), the dot-dot-Operator(..), elements(), children(), children()[i] inside a loop, but nothing works properly. I always get a Null-Object.

       

      For example:

      var xml:XML = load.data; //pseudo
      
      var nodes:XMLList = xml..node; //to get all <node> Elements
      var nodes:XMLList = xml.nodes; //to get the <nodes> Element containing the <node> Elements
      
      var nodes:XML = xml.nodes.node[5];
      var nodes:XML = xml.children()[5].children();
      

      I tried both XML and XMLList. The XML looks something like this:

      <template ...>
           <name>...</name>
           <descr />
           ...
           <nodes>
                <node id="n1">´
                     <name>..</name>
                     <descr/>
                     <activity>
                          <name>...</name>
                          ...
                     </activity>
                     ...
                </node>
                ...
                <node id="n15">...</node>
           </nodes>
           <edges>
                <edge source="n1" dest="n2" ...></edge>
                ...
                <edge source="n3" dest="n14" ...></edge>
           </edges>
           ...
           <startNode>n1</startNode>
           <endNode>n14</endNode>
           ...
           <structuralData>
                <structuralDataNode id="n1" ...>
                     <topological>1</topological>
                     ...
                </structuralDataNode>
                ...
           </structuralData>
      </template>
           
      

       

      It's very deep nested. Inside the <node> Elements are 6 or 7 more levels. But first i just want access to the node-Elements to create Objects with the contained information without splitting the whole XML file. When i load seperate XML files (with nodes or edges as root) it works fine, but then i have to handle 6 or 7 URLLoader. The XML file is attached.

       

      Can anybody please help me? Thanks in advance!

       

      Stefan

        • 1. Re: Problem with accessing XML in ActionScript 3.0
          _Natasha_ Level 4

          Hi,

          are you sure that this string works

          var xml:XML = load.data; //pseudo

          may be

          var xmlData:XML = XML(load.data); // it's not a good idea to use xml for variable name. Check if it's not null here.

          • 2. Re: Problem with accessing XML in ActionScript 3.0
            thrill-house Level 1

            Hi

             

            thanks for your fast answer.

             

            I just wrote this to shorten things up. This is my source code for loading the xml

            var req:URLRequest = new URLRequest("assets/workflow.xml");
            var loader:URLLoader = new URLLoader(req);
            var template:XML = new XML();
            
            /* ... other variables ... */
            
            public function xmlParser():void
            {
                 loader.addEventListener(Event.COMPLETE, XMLLoaded);
            }
            
            private function XMLLoaded(event:Event):void
            {
                 this.template = XML(loader.data);
                 this.parseXML();
            }
            
            /* ... other methods ... */
            
            private function parseXML():void
            {
                 /*...*/
            }
            

            Sorry if i wasn't precise enough.

            • 3. Re: Problem with accessing XML in ActionScript 3.0
              thrill-house Level 1

              I found a good but not satisfying Solution. I cut off the root and loaded all children into a new XMLList Object. So far okay.

              public function testParser()
              {
                   load.addEventListener(Event.COMPLETE, loaded);
              }
              
              private function loaded(event:Event):void
              {
                   this.test = XML(load.data);
                   this.startParsing();
              }
              
              private function startParsing():void
              {
                   //cut the root
                   var noRoot:XMLList = test.children();
                             
                   //put elements in spec array
                   for each(var element:XML in noRoot[5].elements()) this.nodes.push(element);
                   /*...*/
              }
              

              But how can i access 'noRoot' for the spec. Elements instead of using noRoot[5]?

               

              In my case noRoot[5] contains XML:

               

              <nodes>
                   <node id="n1">
                        ...
                   <node>
                   <node id="n2">
                        ...
                   </node>
                   ...
                   <node id="n16">
                        ...
                   </node>
              </nodes>