June 8, 2009
Tips & Tricks 2: Loading data from XML file in Actionscript 2.0
For today’s trick I will show you how easily you can load data from an XML file, using Actionscript 2.0. First thing first you must create an XML file. I named it “image.xml” and save it in the same folder as my flash file.
Here is an example on how your xml file should look like:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<img image="photos/pic1.jpg" description="This is the first image" />
<img image="photos/pic2.jpg" description="This is the second image" />
<img image="photos/pic3.jpg" description="This is the third image" />
</images>
Now create a flash file and paste this code in first frame in the Actionscript tab:
var xmlFile = "images.xml"; //variable with the xml file name
var xml:XML = new XML(); // xml object
xml.ignoreWhite = true;// text nodes that contain only white space are discarded during the parsing process
var image = [];//an array that will store the path attribute of the image from xml
var description = [];//an array that will save the description attribute of the image from xml
xml.load(xmlFile);//load the xml file from the specified URL(image.xml)
xml.onLoad = function() {
var nodes = this.firstChild.childNodes;//hold the nodes from the xml file
var numItems = nodes.length;//number of nodes from the xml file, excepted the root
for (var i = 0; i<numItems; i++) {
var attr = nodes[i].attributes;//for each node hold the attributes
//Store the paths from xml
image[i] = attr.image;//hold the image path attribute
description[i] = attr.description;//hold the description attribute
trace("Path image: "+image[i]+" ------description: "+description[i]);
}
};
That’s all, use it wisely!







Thank you! very useful! Great work!
Leave a Comment