Read XML content using action script 3

1. Create a flash file with actionscript 3

2. Sample XML file format
Code:
<?xml version=”1.0″ ?>
<playlist>
<song title=”Title01″ artist=”ArtistName01″ src=”Song01.mp3″ />
<song title=”Title02″ artist=”ArtistName02″ src=”Song02.mp3″ />
</playlist>

3. In the first frame for the layer 1, write a action script code.
a. Create a XML variable
b. create a URLloader object for load the xml file

Code:
var _xml:XML;
var myLoader:URLLoader = new URLLoader();

4. Load the xml file into URLLoader object
Code:
myLoader.load(new URLRequest(“playlist.xml”));

5. Add a event lister for URLLoader object.

Code:
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
_xml = new XML(e.target.data);
trace(_xml); // get a complete xml
trace(_xml.*); // get the child node
// get the child node attributes
trace(_xml.*.@title[0]);
trace(_xml.*.@artist[0]);
trace(_xml.*.@src[0]);
}

6. Check the source file as attachment

admin

Back to top