ActionScript 3.0 : Getting Data from Uncached Dynamic XML
Today’s tutorial is a bit complex, but it solved a lot of issues faced by flash developer in AS3.
We will be touching on various subjects such as: -
- Creating xml schema of your own.
- Easier way of doing xml data retrieval AS3 style.
- Loads the xml data into Listbox class.
NOTE : Listbox or List is a component in Flash so, it needs a separate import and special event Listener for that. You will get an error if you try to apply regular addEventListener like MouseEvent to it.
See the working sample below.
Below are the sample XML schema. Noticed I have a special settings tag with an attribute called alpha. We will get back to it shortly.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8"?> <countries> <settings alpha="100">-</settings> <country data="MY">Malaysia</country> <country data="SG">Singapore</country> <country data="EG">Egpyt</country> <country data="TH">Thailand</country> <country data="PH">Philippines</country> <country data="IN">Indonesia</country> <country data="BN">Brunei</country> <country data="CN">China</country> <country data="US">USA</country> </countries> |
Sample source code for document class (listboxClass.as)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | package{ import flash.display.*; import flash.events.*; import flash.text.*; import fl.controls.List; import fl.events.ListEvent; import flash.net.*; import fl.data.DataProvider; import flash.text.TextField; public class listboxClass extends MovieClip{ public var MylistBox:List = new List(); public var XmlFile:String = "dataxml.xml"; public var mainXML:XML; public var CountryArray = new Array; public var xmlLoader:URLLoader; public var dp:DataProvider = new DataProvider(); public var tf:TextField = new TextField(); public function listboxClass(){ //We start by adding a Listbox component on the stage. MylistBox.x = 5; MylistBox.y = 30; MylistBox.width = 230; MylistBox.height = 158; addChild(MylistBox); //Place an Indicator; MylistBox.addItem({label:"Loading Data...", data:""}); //Add a textfield to retrieve status and value of listbox tf.width = 220; tf.height = 22; tf.x = 8; tf.y = 230; tf.border = true; addChild(tf); LoadXML(); } public function LoadXML(){ //Stage 2: Loading the XML location; xmlLoader = new URLLoader(); xmlLoader.addEventListener(Event.COMPLETE, onComplete); xmlLoader.load(new URLRequest(XmlFile)); } private function onComplete(event:Event){ //Stage 3: Once loaded to memory - extract info. MylistBox.removeAll(); try{ mainXML = new XML(xmlLoader.data) //Country List is a List to Array based on Scheme tag <country> var CountryList:XMLList = new XMLList(mainXML.country); for(var i:int=0;i<CountryList.length();i++){ dp.addItem({label: CountryList[i], data: CountryList[i].@data}); } //After we got all the data, assign the array to List. Notice every //component in Flash has an unique event Listener. //You will get an error if you use generic event listener for component MylistBox.dataProvider = dp; MylistBox.addEventListener(ListEvent.ITEM_CLICK,triggerDisplayData); //You can also directly point to a tag and access attributes. trace(mainXML.settings.@alpha); //Now that you know the alpha value of the xml. You can apply to our ListBox MylistBox.alpha = mainXML.settings.@alpha; }catch(e:Error){ trace("Error: " + e.message) return; } } private function triggerDisplayData(evt:ListEvent):void{ trace(evt.item.data); tf.text = evt.item.data; } } } |
Download here for full working source files. Download
FYI, for those interested in doing uncached version of xml. Just place a timestamp querystring like this behind XmlFile.
1 2 3 4 5 6 | public function LoadXML(){ //Stage 2: Loading the XML location; xmlLoader = new URLLoader(); xmlLoader.addEventListener(Event.COMPLETE, onComplete); xmlLoader.load(new URLRequest(XmlFile + "?" + Math.random())); } |
However, once you compile you will get a warning error saying the file is invalid. Just ignore that, you won’t get error once you embed the flash online.


























emz said
am September 24 2008 @ 6:42 pm
hi there! you’ve got a really nice blog.. very informative:) i dropped on your ec today. hope you can visit my blog and drop and ec too.. happy wednesday!