Pages

Wednesday, August 3, 2011

AS3: How to load XML in swf at run-time ... Alternate version

We can load xml file into swf at run-time by using following class. Here is an alternate version of xml loading class that I'd presented in my previous post [AS3: How to load XML in swf at run-time].

CODE
package
{
 import flash.display.Sprite;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.events.IEventDispatcher;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
 import flash.events.HTTPStatusEvent;
 import flash.events.IOErrorEvent;

 /**
  * @file CXmlLoader.as
  * @author Abhishek Kumar
  */
 public class CXmlLoader extends Sprite
 {
  public var _Proceed:Function;

  public function CXmlLoader ()
  {
    trace ("CXmlLoader -> constructor");
  }

  public function retrieve (iXml:String):void
  {
    var loader:URLLoader = new URLLoader();
    configureListeners (loader);

    var request:URLRequest = new URLRequest(iXml);
    try
    {
      loader.load (request);
    }
    catch (error:Error)
    {
      trace ("CXmlLoader -> retrieve: " + error.message);
    }
  }

  private function configureListeners (dispatcher:IEventDispatcher):void
  {
    dispatcher.addEventListener (Event.COMPLETE, completeHandler);
    dispatcher.addEventListener (Event.OPEN, openHandler);
    dispatcher.addEventListener (ProgressEvent.PROGRESS, progressHandler);
    dispatcher.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    dispatcher.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    dispatcher.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);
  }

  private function completeHandler (event:Event):void
  {
    trace ("CXmlLoader -> completeHandler: " + event);
    _Proceed (event.target.data);
  }

  private function openHandler (event:Event):void
  {
    trace ("CXmlLoader -> openHandler: " + event);
  }

  private function progressHandler (event:ProgressEvent):void
  {
    trace ("CXmlLoader -> progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
  }

  private function securityErrorHandler (event:SecurityErrorEvent):void
  {
    trace ("CXmlLoader -> securityErrorHandler: " + event);
  }

  private function httpStatusHandler (event:HTTPStatusEvent):void
  {
    trace ("CXmlLoader -> httpStatusHandler: " + event);
  }

  private function ioErrorHandler (event:IOErrorEvent):void
  {
    trace ("CXmlLoader -> ioErrorHandler: " + event);
  }
 }
}

No comments:

Post a Comment