AS3: Simple XML Feed Class
This tip will help you create an easy to use, re-usable class that will set up an external XML feed to use E4X, datagrids, etc. I didn’t change any variable names from my working code, so feel free to change them as needed as the variable names don’t relate to any particular thing…
Download an entire Flash Project using this class here.
To start off we create our package, using my domain name for example, but change this as needed:
package com.connatserdev{ public class feedAggregator extends MovieClip { //Collects argument for XML file path and begins the feed load public function feedAggregator(fmapURL:String) { } } }
Then we call in the needed classes, this goes above the class:
//Flash Class imports import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.ProgressEvent; import flash.display.MovieClip;
Now we set up a couple of variables, one to instantiate an XML object, and one to set a load checking variable. We can use the public static var type so we can access the variables globally.
//Assigning the variables as public static lets us use them globally public static var fmapList:XML = new XML(); public static var feedisLoaded:Number = 0;
Finally we set up our function, creating the request, loader, and the loading the external XML file information into the newly formed XML object. I like to use a “loaded” method to let my parent document know that the feed is ready to be used, this example uses the Number variable: feedisLoaded.
//EVENT CALL var fmapURL:String = fmapURL; var fmapXMLURL:URLRequest; var fmapLoader:URLLoader; fmapList.ignoreWhite = true; fmapXMLURL = new URLRequest(fmapURL); fmapLoader = new URLLoader(fmapXMLURL); fmapLoader.addEventListener(Event.COMPLETE,fmapLoaded); //Once the XML is loaded this method gets called to assign the loaded data to the XML object that we created //Also sets the feedisLoaded variable to 1, can be called in with an EnterFrame event. function fmapLoaded():void { fmapList = XML(fmapLoader.data); feedisLoaded = 1; } //
Now, here is the entire class that you can copy and paste to use. To use this class in your Flash project you will need to create a new variable (var myFeed:feedAggregator = new feedAggregator(“path to your xml file”); Then add that child (addChild(myFeed);)Then you can use feedAggregator.fmapList.* etc using E4X to call attributes in your XML file.
package com.connatserdev{ //Flash Class imports import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.ProgressEvent; import flash.display.MovieClip; public class feedAggregator extends MovieClip { //Assigning the variables as public static lets us use them globally public static var fmapList:XML = new XML(); public static var feedisLoaded:Number = 0; //Collects argument for XML file path and begins the feed load public function feedAggregator(fmapURL:String) { //EVENT CALL var fmapURL:String = fmapURL; var fmapXMLURL:URLRequest; var fmapLoader:URLLoader; fmapList.ignoreWhite = true; fmapXMLURL = new URLRequest(fmapURL); fmapLoader = new URLLoader(fmapXMLURL); fmapLoader.addEventListener(Event.COMPLETE,fmapLoaded); //Once the XML is loaded this method gets called to assign the loaded data to the XML object that we created //Also sets the feedisLoaded variable to 1, can be called in with an EnterFrame event. function fmapLoaded():void { fmapList = XML(fmapLoader.data); feedisLoaded = 1; } // } } }
You can also use the code block below in your document class to check to see if the feed is ready to use. This can let you use a simple loading animation until the value is = 1.
addEventListener(Event.ENTER_FRAME,traceFeed); function traceFeed(e:Event):void { switch (feedAggregator.feedisLoaded) { case 1 : removeEventListener(Event.ENTER_FRAME, traceFeed); break; } }


Recent Comments
connatser on Flash and FDT: Simple workflow video:
Thanks Lee, that's cool to here about the new features with FB4, Bridge...
Lee Probert on Flash and FDT: Simple workflow video:
you could use ANT to automate the publishing of the FLA.New FlashBu...
Wouter on Flex Framework: RobotLegs simple demo:
thanks for this! It helps to get started with RobotLegs. I started working ...
AndreiTT on git: Simple video tutorial:
i find this tutorial very useful, especially the .gitignore tip. thanks...