3 Replies Latest reply on Aug 21, 2007 2:58 PM by journeydude

    Correct Event Sequence for XML Loading

    journeydude Level 1
      I've got a complex object in Actionscript that I've been successful testing and loading on a local machine. However, when I'm publishing to the web, different network conditions result in the application trying to access properties of the complex object before the XML file that defines it is finished processing. As a result I'm getting null reference errors.

      I'm trying to figure out the correct event system to notify my application that I only want to access properties of my complex object AFTER the XML file has been finished read in and populated the properties of my complex object. Any suggestions on what to do?

      I really wish these forums had a code formatting tag or something so this looks horrible, but in general here's what I have:

      package {

      import flash.events.Event;
      import flash.events.IEventDispatcher;
      import flash.net.URLLoader;
      import flash.net.URLRequest;

      public class ComplexObject {
      private var xmlDoc:XML;
      private var request:URLRequest;
      private var loader:URLLoader;

      private var property1:String;
      private var property2:Number;
      //Setter and getter functions assumed
      public function loadData() {
      this.request = new URLRequest("config.xml");
      this.loader = new URLLoader(this.request);
      private function xmlloaded(evt:Event) {
      this.xmlDoc = XML(this.loader.data);
      //populate the object with items read in from XML file
      //this.property1 = xmlDoc.property1

      So I can import and instantiate an object of this class in the main MXML file, and call the loadData function just fine locally. When there is network issues involved (i.e over the web) I want the xml processing to happen independently and complete before I do anything else. What is the proper sequence of event handling? Maybe my question is too complex, but any advice or pointers to resources would be appreciated.
        • 1. Re: Correct Event Sequence for XML Loading
          peterent Level 2
          I don't see anything obviously "wrong" with what you are doing; the Complete event shouldn't be dispatched until the data has been loaded. I don't see where you are making the actual load request.

          However, I wouldn't use this technique to load XML data; there are easier ways. I would use HTTPService with resultFormat="e4x". The HTTPService's result event will be fired when the data has completely loaded. You can either do this in MXML or in ActionScript.
          • 2. Re: Correct Event Sequence for XML Loading
            ntsiii Level 3
            "...use HTTPService with resultFormat="e4x..." I agree completely. Though you are close to this in what you have and URLLoader is very similar to HTTPService, HTTPService is designed spedifically for this usage and is a bit easier to use.

            Like Peter says, what you have should not have any issues with network timing, and switching to HTTPService won't change the behavior, though.

            How are you determining that you are having a problem? Have you done a trace() in the result handler to verify your data? trace(this.xmlDoc.toXMLString()). Use toXMLString() ALWAYS with xml.

            • 3. Re: Correct Event Sequence for XML Loading
              journeydude Level 1
              I was getting null object references because my complex object was responsible for loading the xml data and exposing the data as properties and other objects within faster than the XML data could load. My way of thinking, I guess was that it was logical for a configuration object as I've had it to be responsible for loading its own configuration file. I've separated this out so that the main MXML app loads the configuration XML data using the HTTPService and then fires off the creation of the object and no problems now (fingers and toes crossed).

              But even then, what would I have had to do to get an object that is its own data loader to do in order to fire off an event that it was done doing the loading of the data and have it 'trickle' up to the main application to signal that it was done? The whole event system is easy to understand when someone else has written the code for it, but when it comes to doing it on my own I need way more practice, it seems :-)