3 Replies Latest reply on Jul 22, 2007 11:56 PM by garigantua

    Loading XML in AS3

    backpages
      OK, so I think I have the basics of loading XML in AS3. But I can't seem to access the info once it is loaded. I'm using the to load the .xml file:

      var myXML:XML = new XML();
      var XML_URL:String = "music.xml";
      var myXMLURL:URLRequest = new URLRequest(XML_URL);
      var myLoader:URLLoader = new URLLoader(myXMLURL);
      myLoader.addEventListener("complete", xmlLoaded);

      function xmlLoaded(event:Event):void
      {
      myXML = XML(myLoader.data);
      trace("Data loaded.");
      }

      But when I go to trace the .xml file with this:

      trace(myXML.toXMLString());

      I get nothing. However, if I place this trace inside the xmlLoaded function it traces it out. My problem is I want access to the info outside of that function. What am I doing wrong? Any advice or help would be greatly appreciated. Thanks.
        • 1. Re: Loading XML in AS3
          Level 7
          backpages,

          > But when I go to trace the .xml file with this:
          >
          > trace(myXML.toXMLString());
          >
          > I get nothing.

          This looks like a matter of timing. Your first five lines are
          encountered one after the other, starting with var myXML through
          myLoader.addEventListener(), which calls a function defined immediately
          below that. The function is called when the "complete" event is dispatched,
          which may take longer or shorter depending on the size of the XML document
          and network traffic. If you put a trace() statement immediately below that,
          Flash will execute that too -- and at the moment it does, your XML document
          hasn't loaded yet.

          > However, if I place this trace inside the xmlLoaded function it
          > traces it out. My problem is I want access to the info outside
          > of that function. What am I doing wrong?

          I copy/pasted your code exactly, then followed it with this:

          var t:Timer = new Timer(5000);
          t.addEventListener(
          TimerEvent.TIMER,
          function():void {
          trace(myXML);
          }
          );
          t.start();

          ... which waits five seconds, then runs that trace(). Sure enough, it
          outputs the XML contents.

          If you want to use the myXML variable elsewhere in your movie, you just
          have to make sure those other locations don't reference the variable until
          it has a value (or account for a lack of value in the logic they use). You
          might, for example, put a stop() action in frame one, along with the
          existing code (and instead of my timer), then have the "complete" event
          handler issue a play() action. In later frames, the myXML variable would
          have the data you need, because the playhead won't advance until the data
          have been loaded.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: Loading XML in AS3
            backpages Level 1
            Perfect! Thank you very much David for the info. What I ended up doing what you suggested, putting a gotoAndStop() event after the info had loaded and then just started the rest of the movie a few frames down. Works great. Thanks again.
            • 3. Loading XML in AS3
              garigantua Level 1
              On a similar topic:

              When an XML is being loaded from an imported class, timing also plays a role. The event listener should not be placed from the URLLoader-type property of the custom class, but rather the instance of the class.
              ie

              var instanceXML = new LoadXML(); //where LoadXML is a custom class that does the URL request and XML load.

              var instanceXML .loader.addEventListener( Event.COMPLETE, XMLLoadComplete ); //where "loader" is a property type URLLoader within the LoadXML class.

              Instead of adding the event listener on the loader property within the LoadXML class (as many tutorials would have you do), the event listener should be placed on the loader property of the instance. Parsing a lengthy XML would cause empty variables to be passed before the load has terminated. Hope this is clear and that it helps someone as it had me stuck for a few hours.

              Thanks David for the clue.