Parsing an XML File with Attributes
3 comments so farCoincidently I had a new class to load in XML files and I had a comment/request to demonstrate how to read an XML file that has attributes. Not much of a demo, so here are just the class files and the xml file… I used these classes for an mp3 player that I constructed.
The XML file contents (Just noticed that the code looks screwy.. here is the xml file: http://manewc.com/projects/flash/XMLAttributes/files-xml/mp3player.xml)
The class that will load the XML file called LoadXML.as
package
{
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IEventDispatcher;
import flash.events.SecurityErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.xml.XMLDocument;
public class LoadXML extends Sprite
{
public var loadStatus:Boolean =false;
public var xml:XML;
public function LoadXML(xmlurl:String):void {
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest(xmlurl);
try {
loader.load(request);
} catch (error:Error) {
trace ("Unable to load requested document.");
}
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event)
{
xml = new XML(event.target.data);
loadStatus = true;
}
public function LoadComplete()
{
return loadStatus;
}
public function GetXML()
{
return xml;
}
private function openHandler(event:Event):void {
trace ("XML openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace ("XML progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace ("XML securityErrorHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace ("XML httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace ("XML ioErrorHandler: " + event);
}
}
}
A class for a Song Object
package
{
import flash.display.Sprite;
public class Song extends Sprite
{
// public properties for the class
public var earl:String;
public var artist:String;
public var track:String;
public var coverimage:String;
public var genre:String;
public function Song(e:String, a:String, t:String, c:String, g:String)
{
earl = e;
artist = a;
track = t;
coverimage = c;
genre = g;
}
}
}
and of course the Main.as Document Class file:
package
{
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var getXML:LoadXML;
private var ct:Number = 0;
private var sa:Array;
private var urlList:XMLList;
private var artistList:XMLList;
private var trackList:XMLList;
private var coverimageList:XMLList;
private var genreList:XMLList;
public function Main()
{
init();
}
private function init():void
{
// setup the XML and the Song List
getXML = new LoadXML("files-xml/mp3player.xml");
addChild ( getXML );
// track the load progress
addEventListener(Event.ENTER_FRAME, CheckXMLLoad);
}
private function CheckXMLLoad(e:Event):void
{
if ( getXML.LoadComplete() ) {
removeEventListener(Event.ENTER_FRAME, CheckXMLLoad);
BuildSongList ( getXML.GetXML() );
}
}
private function BuildSongList( xml:XML ):void
{
// Gather the attribute data
var urlList:XMLList = getXML.xml.song.@url;
var artistList:XMLList = getXML.xml.song.@artist;
var trackList:XMLList = getXML.xml.song.@track;
var coverimageList:XMLList = getXML.xml.song.@coverimage;
var genreList:XMLList = getXML.xml.song.@genre;
sa = new Array();
for each (var urlXML:XML in urlList)
{
sa.push(new Song(urlList[ct], artistList[ct], trackList[ct], coverimageList[ct], genreList[ct]));
ct++;
}
traceData();
}
private function traceData():void
{
for ( var a:uint = 0; a < sa.length; a++ )
{
trace ( "\n" + a + ". ----------------------- ")
trace ( "Earl: " + sa[a].earl + "\n");
trace ( "Artist: " + sa[a].artist + "\n");
trace ( "Track: " + sa[a].track + "\n");
trace ( "CoverImage: " + sa[a].coverimage + "\n");
trace ( "Genre: " + sa[a].genre + "\n");
}
}
}
}
Tuesday, June 3rd, 2008 at 10:13 am and is filed under Actionscript 3, Actionscript XML. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Hey this a great article…………
How do you access attributes in JS + Air? The @attributename syntax obviously can’t be parsed by the JavaScript engine. According to the manual, XML node attributes can be extracted like this:
“myXML.channel.item[1].enclosure.attributes.url” but it displays an empty string, while “myXML.channel.item[1].title” displays the node value just fine. I’m trying to parse podcast RSS feeds by the way. Thanks!
Love this article…in depth and full use of the XML - classes and error-handling in AS3.
Took me a while to find it. Was trying some AS3 today coming from AS2, where i used this same principle of “classing” attributes of XML. AS3 seem to have made it more complicated (at first glance).
Another possiblilty here seems to move the code (like the method BuildSongList() ) more in the LoadXML.as and then dispatch an event (if loaded). And then retrieving the array with the Song-classes to the Main.as. That’s taking away the speed-consuming Event.ENTER_FRAME.
But understand this example is for the principle…And it’s perfect..The best I found so far.
Thanks a lot. Erik