If you want to load external XML file in flash, first you will need to import certain flash Classes.
Let’s see which kind of flash classes we need:
- URLLoader Class – With this class we can download data from url as Text or binary data or URL-encoded variables. Now in this tutorial we will load our XML file with this Class.
- URLRequest Class – This class is also important in this process because this class will initiate our URLLoader Class to start load file.
- Event Class – Event class will notify us when the URLLoader is finished with loading or if we have the error in loading process.
- XML Class – XML class we dont need to import because this is a TOP Level class such as Array,Number,Boolean,Object.
TIP: See more detail in flash help about this Classes for better understanding!!!
Ok Let’s start with the coding:
//import necessary classes
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;var _xml:XML;
var xmlLoader:URLLoader = new URLLoader();
var xmlURL:URLRequest = new URLRequest(“sources/xml/datas.xml”);xmlLoader.load(xmlURL);
//now I will add event listener to URLLoader Class, first parameter in addEventListener function indicate event type, second parameter refer on function which will handle our “complete” event
xmlLoader.addEventListener(Event.COMPLETE, _completeXML);//event handler will be called when URLLoader finished loading process.
function _completeXML(event:Event):void {//now I will forward data from URLLoader class to XML Class
//event.target is class what is dispatch “complete” event, in our example that is URLLoader Class
_xml = new XML(event.target.data);
_xml.ignoreWhitespace = true; //igore white space in xml
_xml.ignoreComments = true; //ignore comments in xml
trace(_xml);
}
That is that external XML file is now loaded in flash and we can now start to get data from XML.
You can download source file here with previous tutorial(How to create Document Class).
SOURCE DOWNLOAD: LoadXML_AS3.zip
TIP: See more detail in flash help about this Classes for better understanding!!!
That’s really good with limited code for use external xml files.
Thanks
Uday
uday_sgh at yahoo
Sr. Flash Developer
Hyderabad