O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loading XML Data

For instructive purposes, most XML examples in this chapter have been written in literal form. However, in real applications it's much more common to load XML from an external source.

To load external XML data into an XML instance, follow these general steps:

  1. Create a URLRequest object describing the location of the external XML (either a file or a server-side script that returns XML).

  2. Create a URLLoader object, and use its load( ) method to load the XML.

  3. Wait for the XML to load.

  4. Pass the loaded XML to the constructor of a new XML instance.

While a full discussion of the URLRequest and URLLoader classes is beyond the scope of this chapter, Example 18-13 demonstrates the code required to load XML data into an XML instance. The class in the example, XMLLoader, extends Sprite so that it can be compiled as an application's main class for testing. For information on the URLRequest and URLLoader classes see Adobe's ActionScript Language Reference. For information on event handling, see Chapter 12.

Example 18-13. Loading external XML

package { import flash.display.*; import flash.events.*; import flash.net.*; // Demonstrates the code required to load external XML public class XMLLoader extends Sprite { // The variable to which the loaded XML will be assigned private var novel:XML; // The object used to load the XML private var urlLoader:URLLoader; // Constructor public function XMLLoader () { // Specify the location of the external XML var urlRequest:URLRequest = new URLRequest("novel.xml"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required