Below is a tutorial on how you can create an associative array within your Actionscript that will model itself from the contents of an XML file. I actually dug quite a bit on the net looking for this information, and realized that after I failed to do so, that I would just have to figure it out myself. So, below you will find information on how this can be achieved using ActionScript...
First thing you will need to do is to create a temporary XML file next in the same directory as your Flash Project file called videos.xml. In this file, you will place the following...
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<track>
<title>Video 1</title>
<location>video1.flv</location>
</track>
<track>
<title>Video 2</title>
<location>video2.flv</location>
</track>
<track>
<title>Video 3</title>
<location>video3.flv</location>
</track>
</trackList>
</playlist>Now that you have done this, you can then use the following code to extract the information from that XML file and place all the data that you will need in an associative array for easy access within your ActionScript code. Basically, what I am saying is that I would like to take the information above and create the following array out of it....
var videos:Array = new Array(
new Array({title:"Video 1", location:"video1.flv"}),
new Array({title:"Video 2", location:"video2.flv"}),
new Array({title:"Video 3", location:"video3.flv"})
);Although... instead of it being a static array in my ActionScript, I want to be able to have this array dynamically create itself from the XML. You can do this by using the XML class within ActionScript. Below, I have included the source to parse the information above. Pay close attention to the comments, because I walk you through each step in the comments. I hope this helps someone out there.
// Declare our array that will be storing the videos.
var videos:Array;
// Declare our XML variable that will contain our XML information.
var videosXML:XML = new XML();
// Load our XML file...
var videosXML.load("videos.xml");
// Called when the XML data loads.
videosXML.onLoad = function() {
// Declare our index variable to keep track of our videos.
var index:Number = 0;
// Set the size of our video array to the amount of children under the "trackList" tag.
// this.firstChild - points to the "playlist" tag for the XML file.
// this.firstChild.firstChild - points to the "trackList" tag for the XML file...
videos = new Array(this.firstChild.firstChild.childNodes.length);
// Iterate through all of the tracks starting with the first Child under "trackList".
for (var aNode:XMLNode = this.firstChild.firstChild.firstChild; aNode != null; aNode = aNode.nextSibling) {
// Declare another array in this array to hold our associative array.
// aNode.firstChild - points to the "track" tag for this iteration of the "trackList".
videos[index] = new Array(aNode.firstChild.childNodes.length);
// Iterate through all of the nodes within each track listing. For our example, they are "title" and "location"
for (var subNode:XMLNode = aNode.firstChild; subNode != null; subNode = subNode.nextSibling) {
// Set the video at this index with the association of the tag name...
// The nodeNames ..."track" or "location".
videos[index][subNode.nodeName] = subNode.firstChild.nodeValue;
}
// Increment the video index.
index++;
}
// At this point, you now have an array of videos where you can grab the information of each video like so...
// Get the first videos location....
trace(videos[0]["location"]);
// Get the second videos title....
trace(videos[1]["title"]);
}




Associative Array
I like it. Found two things I needed to fix to get it to work, tho.
1. delete var from the load XML line: videosXML.load("videos.xml");
2. need to tell it to ignore white space: videosXML.ignoreWhite=true;
Nice, clean code
Tons of help. Thanks!
You got hard work, thank
You got hard work, thank you.
______
bags