Copy link to clipboard
Copied
I have an XML file that I want to be able to load into an array and then be able to call on potions of the array to fill text boxes etc as needed through the flash movie. My question is this. Does the folowing code do it or do I need to load it into the array a different way? So example, I would query the array for TestTrack 2 and if it exists in the array I want to use all parts of it. (title, band, and file). Thanks for the help.
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<playlist>
<track>
<title>TestTrack 1</title>
<band>Band1</band>
<file>test1.mp3</file>
</track>
<track>
<title>TestTrack 2</title>
<band>Band2</band>
<file>test2.mp3</file>
</track>
<track>
<title>TestTrack 3</title>
<band>Band3</band>
<file>test3.mp3</file>
</track>
<track>
<title>TestTrack 4</title>
<band>Band4</band>
<file>test4.mp3</file>
</track>
</playlist>
AS3 CODE:
var playlistArray:Array = new Array();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("playlistAS3.xml"));
function showXML(e:Event):void
{
XML.ignoreWhitespace = true;
var songs:XML = new XML(e.target.data);
trace(songs.track.length());
var i:Number;
for (i=0; i < songs.track.length(); i++)
{
playlistArray= songs.children();
/*trace(" Name of the song: "+ songs.track.title.text());
trace(" Name of the Band: "+ songs.track.band.text());
trace(" File Location of the song: "+ songs.track.file.text());*/
}
trace(playlistArray[0]);
}
1 Correct answer
That is filling the array with portions of the xml file, not as data that you can readily access without parsing it further.
What you should do in the loop is assign those items you are tracing as properties of an object and push the object into the array....
playlistArray.push( { title: songs.track.title.text(), band: songs.track.band.text(), track: songs.track.file.text() });
Then later on you can access the data using...
playListArray.title
playListArray.band
playListArray.track
Copy link to clipboard
Copied
That is filling the array with portions of the xml file, not as data that you can readily access without parsing it further.
What you should do in the loop is assign those items you are tracing as properties of an object and push the object into the array....
playlistArray.push( { title: songs.track.title.text(), band: songs.track.band.text(), track: songs.track.file.text() });
Then later on you can access the data using...
playListArray.title
playListArray.band
playListArray.track
Copy link to clipboard
Copied
That works perfect. Thanks.
Copy link to clipboard
Copied
You're welcome

Copy link to clipboard
Copied
While Ned's answer is spot on, there's no need for an array - it actually makes it more work than necessary.
AS3 has the XMLList object made just for this.
In your showXML function you have:
var songs:XML = new XML(e.target.data);
After that you can just make an XMLList object with all the tracks:
var tracks:XMLList = songs.track;
trace(tracks.length()); //4
To get the third title:
trace(tracks[2].title);

