Custom XML loader class

XMLLoaderEvent.as

/**
 * XMLLoaderEvent
 * Description: events sent by the XMLLoader
 */
package
{
 import flash.events.Event;

 public class XMLLoaderEvent extends Event
 {
 public static const XML_PROGRESS:String  = "XML_PROGRESS";
 public static const XML_COMPLETE:String  = "XML_COMPLETE";

 public var _data:*;

 public function XMLLoaderEvent($type:String, $data:* ,$bubbles:Boolean = false, $cancelable:Boolean = false )
 {
 super($type ,$bubbles, $cancelable);

 _data = $data;
 }

 override public function clone():Event
 {
 return new XMLLoaderEvent (type, bubbles, cancelable, _data);
 }

 }
}


XMLLoader.as


/**
 * XMLLoader
 * Description: xml loader
 */
package
{
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.events.ProgressEvent;
 import flash.events.Event;
 import flash.events.EventDispatcher;

 import XMLLoaderEvent;

 public class XMLLoader extends EventDispatcher
 {
 protected var _loader:URLLoader = null;    // loader for the xml

 /** load the xml **/
 public function Load($path:String)
 {
 _loader = new URLLoader();
 _loader.load(new URLRequest($path));
 _loader.addEventListener(ProgressEvent.PROGRESS, XMLLoading, false, 0, true);
 _loader.addEventListener(Event.COMPLETE,         XMLComplete, false, 0, true);
 }

 /** xml loaded**/
 private function XMLComplete($e:Event):void
 {
 dispatchEvent(new XMLLoaderEvent(XMLLoaderEvent.XML_COMPLETE, _loader));
 }
 /** xml is loaded **/
 private function XMLLoading($e:ProgressEvent):void
 {
 dispatchEvent(new XMLLoaderEvent(XMLLoaderEvent.XML_PROGRESS,($e.bytesLoaded/$e.bytesTotal)));
 }
 /** destroy this **/
 public function Clear():void
 {
 _loader.removeEventListener(ProgressEvent.PROGRESS, XMLLoading);
 _loader.removeEventListener(Event.COMPLETE,         XMLComplete);
 _loader = null;
 }
 }
}

 

HOW TO USE IT:


var _xmlLoader:XMLLoader = new XMLLoader();
_xmlLoader.Load("test.xml");
_xmlLoader.addEventListener(XMLLoaderEvent.XML_COMPLETE, Complete, false, 0, true);
_xmlLoader.addEventListener(XMLLoaderEvent.XML_PROGRESS, Progress, false, 0, true);
function Complete($e:XMLLoaderEvent):void
{
 trace ($e._data.data)
}

function Progress($e:XMLLoaderEvent):void
{
//loading
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s