• Loading Text and XML with URLLoader in AS3

    In previous versions of ActionScript, there were a couple of classes who had the capability of loading external text, namely LoadVars and XML. The loading responsibilities of these classes has moved to one single class in ActionScript 3, URLLoader (flash.net.URLLoader). This class is a lot like LoadVars. The big difference is that it is used for XML since the responsibility of loading XML from an external source has been removed from the XML class. Instead, you would load the text with URLLoader and then give that text to an XML object for parsing.

    Like LoadVars, URLLoader has a load() method that is used to load text from an external source. This accepts 1 argument, a URLRequest instance (NOT a URL string). You can then use events from URLLoader to determine when the loading is complete. When complete, the text loaded is available in the data property of URLLoader.

    Example:

    var loader:URLLoader;
    // …
    loader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, xmlLoaded);

    var request:URLRequest = new URLRequest(“file.xml”);
    loader.load(request);
    //…
    function xmlLoaded(event:Event):void {
        var myXML:XML = new XML(loader.data);
        //…
    }

    This entry was posted on Thursday, October 22nd, 2009 at 8:20 am and is filed under ActionScript, Flash, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 0 Comments

    Take a look at some of the responses we've had to this article.

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message: