2 Replies Latest reply on Oct 17, 2009 6:45 AM by Gregory Lafrance

    weird thing on loading xml with actionscript



      i load an xml file using only actionscript (actionscript project). Here is my code:

           import flash.display.Sprite;
           import flash.events.Event;
           import flash.net.URLLoader;
           import flash.net.URLRequest;
           public class asxml extends Sprite
                public var myXML:XML = new XML();
                public var XML_URL:String = "functions.xml";
                public var myXMLURL:URLRequest = new URLRequest(XML_URL);
                public var myLoader:URLLoader = new URLLoader(myXMLURL);
                public var print2:String;
                public function asxml()
                     myLoader.addEventListener("complete", xmlLoaded);
           public function xmlLoaded(event:Event):void 
             myXML = XML(myLoader.data); 


      and here is the xml file:


      <?xml version="1.0" encoding="utf-8"?>
              <item id='1'>
              <item id='2'>


      can someone explain me why in the 1st trace the print2 string hasnt taken its value yet(it returns null), while in the second trace it has taken normally its value from the xml ??

        • 1. Re: weird thing on loading xml with actionscript
          pauland Level 4

          Yes. The second trace in xmlLoaded is only called when the xml data has  finished loading, while the trace in the constructor is most likely to  be called before the xml has loaded.
          It takes time to load a file - longer than it takes to initialise and  call the constructor.

          Generally speaking, you should avoid processing XML like that through  initialisation values - far better to explicitly do these things in the  constructor, or better still in an init() function called by the  constructor.


          1 person found this helpful
          • 2. Re: weird thing on loading xml with actionscript
            Gregory Lafrance Level 6

            The first trace of print2 is null and that's normal, because only when the "complete" event is dispatched is the data available. At the time of the first trace statement, nothing has even been assigned to print2. addEventListener only assigns a listener method, it does not actually do anything, and events are asynchronous in Flex.


            If this post answers your question or helps, please mark it as such.

            1 person found this helpful