Tips & Tricks 2: Loading data from XML file in Actionscript 2.0 June 8, 2009

Tips & Tricks 2: Loading data from XML file in Actionscript 2.0

by flashuser in Actionscript 2.0, Flash Tips & Tricks

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!

Subscribe to our RSS Feed and follow us on Twitter

Enjoy this post?

Help us grow this site and share the content with others among you.

img-delicious img-digg img-twitter
img-stumbleupon img-facebook img-mixx

Comments
  1. Thank you! very useful! Great work!


  1. Loading data from XML file in Actionscript 3.0 | Flash User

Leave a Comment

Note: You can get a Gravatar account for free so your avatar can be shown when you post a comment on any website that supports gravatars.