Tuesday, March 9, 2010

Loading XML File Using the URLLoader Class for Flash

The loading of any textual content such as XML, CSS, and HTML is managed by the URLLoader Class. This class is very simple to use. Start off by creating an instance of it and then use the .load() method to load the playlist.xml file.

var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("playlist.xml"));

This should load your XML file into Flash, but that on its own will not do anything as Flash is still not yet told what to do when the file loads. To take an action once the file is loaded we need to use an event handler to listen to the Event.COMPLETE. Register this event with a function which you will have to create later. Simply add the following code to use the .addEventListener() method to register for this event:

var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("playlist.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);

No comments:

Post a Comment