Home » ActionScript 3.0, Flash CS4, Flex, XML

XMLManager in ActionScript 3.0

13 November 2008 One Comment
Share on Facebook

XMLManager is a utility class that provides a single interface to load XML. Please find the source files at the end of the tutorial.
XMLManager Utility makes work easy to load XML in actionscript3.0, Utility is free to use.
Just pass the URL of the xml as explained in code at timeline at source file to load XML.

XMLManager Class :

/**
  * XMLManager is a utility class that provides a single interface to load XML
  * And dispatches XMLLOADEDCOMPLETE event when the XML loaded
  *
  * Code from -- http://www.designscripting.com/
  * Don't delete the above lines.
  */
package com.Manager
{
  import flash.events.*;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  /**
   * XMLManager extends EventDispatcher to dispatch Event
   */
  public class XMLManager extends EventDispatcher
  {
    /**
     * Static variable XMLLOADEDCOMPLETE for holding the new Event
     */
    public static var XMLLOADEDCOMPLETE : String = "XMLComplete";
    public static var XMLLOADSTATUS : String = "XMLStatus";
    /**
     * private variables
     */
    private var _loader : URLLoader;
    private var _XMLData : XML;
    public var XMLLoadedPercentage : Number;
    /**
     * Function: XMLManager
     * Calls super method which invokes the Super Class   constructor
     */
    public function XMLManager()
    {
      super();
      _loader = new URLLoader();
    }
    /**
     * Function: loadXML
     * @param XMLPath The path of the XML to load.
     * Adding the Complete Event to URLLoader.
     */
    public function loadXML( XMLPath : String ) : void
    {
      _loader.load( new URLRequest( XMLPath ) );
      _loader.addEventListener( Event.COMPLETE , handleXMLLoaded, false, 0, true );
      _loader.addEventListener( ProgressEvent.PROGRESS, handleXMLProgress, false, 0, true);
    }
    /**
     * Function: handleXMLLoaded
     * _XMLData has been set here.
     * Removing the loader Listener.
     * Dispatching new Event "XMLComplete" after XML loading complete
     */
    private function handleXMLLoaded( event:Event ) : void
    {
      _XMLData = XML(event.target.data);
      _loader.removeEventListener( Event.COMPLETE, handleXMLLoaded, false );
      _loader.removeEventListener( Event.COMPLETE, handleXMLProgress, false );
      _loader = null;
      dispatchEvent( new Event( XMLLOADEDCOMPLETE ) );
    }
    /**
     * Function: data
     * Getter function to get data once loaded
     * Returns XML data
     */
    public function get data() : XML
    {
      return _XMLData;
    }
    /**
     * Function: handleXMLProgress
     */
    private function handleXMLProgress( event : ProgressEvent ) : void
    {
      XMLLoadedPercentage = ( event.bytesLoaded/event.bytesTotal )*100;
      dispatchEvent( new Event( XMLLOADSTATUS ) );
    }
  }
}

Code in Timeline:

// Import the XMLManager Class
import com.Manager.XMLManager;
// Creating instance for XMLManager Class
var _xmlmanager : XMLManager = new XMLManager();
// Adding event to handle XML status
_xmlmanager.addEventListener( XMLManager.XMLLOADSTATUS , handleXMLStatus, false, 0, true);
// Adding event to handle XML loaded complete event
_xmlmanager.addEventListener( XMLManager.XMLLOADEDCOMPLETE , handleXMLData, false, 0, true);
// Passing the URL of the XML to load
_xmlmanager.loadXML( "store.xml" );
function handleXMLStatus( event : Event )
{
  trace("XML Loaded percentage:::"+_xmlmanager.XMLLoadedPercentage);
}
// Getting the Loaded XML
function handleXMLData( event : Event )
{
  trace("XML Loaded:::"+_xmlmanager.data);
}

Code Explanation:
Before code explanation lets discuss some basic concepts.
PACKAGE :

Allows you to organize your code into discrete groups that can be imported by other scripts. You must use the package keyword to indicate that a class is a member of a package.
The above definition is from Flash help.
Package is just like folder structure in hard disk.
Here,
package com.Manager
specifies the Class resides inside com which resides inside Manager. Have a look at source.

STATIC VARIABLE:

Static variable is also called as Class variable, since static variables can be accessed through class not through objects.
Static variables cannot be override.
Static variables and instance variable can have the same name in AS3, this is not the case in AS2.

Explanation:

public static var XMLLOADEDCOMPLETE : String = “XMLComplete”;
Holds the event type .

In constructor function we are calling super(); method
Which calls the super class EventDispatcher constructor and initializes the superclass which is extended by the sub class XMLManager.

In general, the easiest way for a user-defined class to gain event dispatching capabilities is to extend EventDispatcher.
The public function loadXML gets the parameter for XML file to be loaded and dispatches Event.Complete Event when the XML loaded.
Dispatches an event into the event flow.
In the load complete event we are dispatching custom event, that €™s used to handle the data loaded.

To learn more about EventFlow or EventHandling in actionscript3.0 click here

In timeline,
We are importing the class XMLManager
And adding the eventlistener for the custom event we are dispatching, in the handler for custom event we are calling the _xmlmanager.data which actually calls the get data method.

Find any trouble in using the utility, please add comments.

Download Source files

-sara

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


One Comment »

  • akilesh said:

    thanks for the utility… its useful but i am getting few error stating that “Access of undefined property handleXMLStatus” and “Access of undefined property _xmlmanager”.. I have imported the XMLManager actionscript class..

    i want to display the data in the datagrid retrieved from the xml file… How do i do that?

    Thanks

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.