Dynamically loading external XML files using ActionScript 3.0

by Peter deHaan on March 2, 2008

in URLLoader, XML, migration

The following examples show how you can load external XML files at runtime using the XML class in ActionScript 2.0 and the URLLoader class in ActionScript 3.0.

Full code after the jump.

The following example shows how you can load an external XML file using ActionScript 2.0, using the XML class:

// ActionScript 2.0
/**
 * Requires:
 *   - Text field on the stage with an instance name of "textArea".
 */
var xml:XML = new XML();
// xml.ignoreWhite = true;
xml.onLoad = function(success:Boolean):Void {
    textArea.text = xml.toString();
}
xml.load("mlb.xml");

The following example shows how you can load an external XML file using ActionScript 3.0, using the URLLoader class:

// ActionScript 3.0
/**
 * Requires:
 *   - Text field on the stage with an instance name of "textArea".
 */
var xml:XML;
 
var urlRequest:URLRequest = new URLRequest("mlb.xml");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
 
function urlLoader_complete(evt:Event):void {
    xml = new XML(evt.target.data);
    textArea.text = xml.toXMLString();
}

{ 10 comments… read them below or add one }

1 Adam 07.25.08 at 6:34 pm

How can I use these same scripts but in a movie clip? It seems to be causing me problems to embed a dynamic text box in a movie clip layer.

2 Uday 11.20.08 at 11:50 am

Here is the good Example:
var imagesXML:XML;
var xmlImagesLoader:URLLoader=new URLLoader;
xmlImagesLoader.load(new URLRequest(xmlFile.xml));
xmlImagesLoader.addEventListener(Event.COMPLETE,loadImagesXML);
var l:Number;
var labelList:Array = new Array();
var tooltipList:Array = new Array();
var imageSourceList:Array = new Array();
var imageLinkList:Array = new Array();

function loadImagesXML(e:Event):void {
imagesXML=new XML(e.target.date);
l=imagesXML.children().length();
for (var i=0; i<l; i++) {
labelList[i] = imagesXML.image[i].label.children();
tooltipList.push(imagesXML.image[i].tooltip.children());
imageSourceList[i] = imagesXML.image[i].imageSource.children();
imageLinkList[i] = imagesXML.image[i].imageLink.children();

}

}

You can use the defined Array to any where to your flash application.

xmlFile.xml

My HOME
MyHOMEPage
images/icon1.jpg
http://google.com

CARBURETORS
HelloBrother
images/icon0.jpg
http://google.com

Let me know if you have any doubt.

Thank,
Uday
uday_sgh@yahoo.com
Sr. Flash Developer
Hyderabad

3 R2-D2 05.12.09 at 1:56 pm

Uday, is as3 there an easy way to define the xml file through html?

In as2 I would treat it like this:
var urlRequest:URLRequest = new URLRequest(datasrc);

and in the html it would look like: myfilename.swf?datasrc=data.xml

Thanks in advance!

4 adarsh 05.29.09 at 2:44 am

hello, actually i want to dynamically access xml file and create tree struture in flex..and xml file having lots of child node

5 deepa 06.05.09 at 7:13 am

I want to send the xmlFile.xml file format. can you please post that file so that i can understand the flow

6 Ely Tejeda 06.24.09 at 9:05 am

Hello, i’ve figured out how to read with flash a xml file generated by php, when you acces to it via a internet browser it generates a different xml file each time. But when im testing my movie with flash it keeps showing me the results as if it loaded the same xml each time, when i restart flash, it loads a different xml but when i test the movie again i get the same result. I think its a problem with the cachee configuration but i dont know exactly what cachee is… ive tried to set useCache=false; but it is a command only aviable for AIR. Any ideas?

7 Peter deHaan 06.24.09 at 7:17 pm

Ely Tejeda,

Try adding some unique token to the PHP script URL. Something like a timestamp.

var targetURL:String = "getXML.php?uid=" + new Date().time;

Peter

8 Oliver 08.20.09 at 10:10 am

Is it possible to use “Loader” instead of “URLLoader?”

9 Peter deHaan 08.20.09 at 11:27 am

Oliver,

I believe the Loader class is used to load SWF or image files, not text files.
http://livedocs.adobe.com/flex/3/langref/flash/display/Loader.html

The Loader class is used to load SWF files or image (JPG, PNG, or GIF) files. Use the load() method to initiate loading. The loaded display object is added as a child of the Loader object.
Use the URLLoader class to load text or binary data.

Peter

10 Brad 10.26.09 at 10:33 am

Nice article. How would one load an external XML file if there is just a URL which spits out XML? For example http://server/blah/xml.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: