« Flash CS3 AS3: doubleClickEnabled "Gotcha" | ^ Main | Desert Code Camp - September 15, 2007 »


AS3 XMLDataLoader

Unlike my previous AS2 version, this one is pretty much nothing but XML data loading, literally. I’ll get right to it and put the code in here. First, here’s how you use it:

//this would go somewhere in a class or on the timeline
XMLDataLoader.instance.addEventListener(XMLDataLoader.instance.LOAD_COMPLETE, parseMyData);
XMLDataLoader.instance.url = “my.xml”;
XMLDataLoader.instance.trigger();

private function parseMyData(event:XMLDataEvent):void{
     trace(event.data); //traces the XML data
}

Here is the XMLDataLoader class:

/**
* XML Data Loader
* @author John C. Bland II
* @version 0.1
*/

package classes.data {
    import flash.net.*;
    import flash.events.*;
    import classes.events.XMLDataEvent;
    public class XMLDataLoader extends DataLoader{
        private var _url:String = “”;
        public function get url():String{ return _url; }
        public function set url(value:String):void{ _url = value; }
        private var _loader:URLLoader = new URLLoader();
        public function XMLDataLoader(enforcer:SingletonEnforcer){
            if(enforcer null){
                throw new ArgumentError(“XMLDataLoader is a Singleton class. Use XMLDataLoader.instance.”);
                return;
            }
            _loader.addEventListener(Event.COMPLETE, onLoadComplete);
        }
        private function onLoadComplete(event:Event):void{
            var ev:XMLDataEvent = new XMLDataEvent(LOAD_COMPLETE);
            ev.data = new XML(_loader.data);
            dispatchEvent(ev);
        }
        private function onLoadFailed(event:Event):void{
            dispatchEvent(new Event(LOAD_FAILED));
        }
        public function trigger():void{
            if(_url.length 0){
                throw new Error(“XMLDataLoader.url must be set before calling trigger()”);
                return;
            }
            _loader.load(new URLRequest(_url));
        }
        /**
        * Singleton implementation
        */
        private static var _instance:XMLDataLoader;
        public static function get instance():XMLDataLoader{
            if(_instance == null) _instance = new XMLDataLoader(new SingletonEnforcer);
            return _instance;
        }
    }
}

class SingletonEnforcer{}

This is a Singleton so be sure to keep the SingletonEnforcer at the bottom of the file (inaccessible to external code). Notice there is an XMLDataEvent, which is custom.

/**
* XMLDataEvent
* @author John C. Bland II
* @version 0.1
*/

package classes.events {
    import flash.events.Event;

    public class XMLDataEvent extends Event{
        private var _data:XML;
        public function get data():XML{ return _data; }
        public function set data(value:XML):void{ _data = value; }
        public function XMLDataEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false){
            super(type, bubbles, cancelable);
        }
    }
}

That’s it. In a nutshell, add an event listener, set the url, trigger the data load, and do whatever you want with the data when it is completely loaded.

Enjoy.

Posted by John C. Bland II on July 11, 2007 10:50 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/289

Post a comment