package{
//net
import flash.net.URLLoader;
import flash.net.URLRequest;
//events
import flash.events.Event;
import flash.events.EventDispatcher;
public class XMLLoader extends EventDispatcher{
private var _URLLoader:URLLoader;
private var _URLRequest:URLRequest;
private var _XMLFiles:Array;
private var _XML:XML;
private var _XMLFilesPointer:int = 0;
//private var _XMLPath:String = "xml/vday-music.xml";
//Custom Dispatchers
public static var MUSIC_XML_LOADED:String = "music_xml_loaded";
public static var VIDEO_XML_LOADED:String = "video_xml_loaded";
public function XMLLoader():void{
_XMLFiles = new Array('xml/vday-music.xml','xml/vday-video.xml');
_XML = new XML("<playlist></playlist>");
}//end of constructor
public function loadXML():void{
_URLLoader = new URLLoader();
_URLRequest = new URLRequest(_XMLFiles[_XMLFilesPointer]);
_URLLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);
_URLLoader.load(_URLRequest);
}
//Event Handlers
private function xmlLoadComplete(e:Event):void{
_URLLoader.removeEventListener(Event.COMPLETE, xmlLoadComplete);
var xml = XML(e.target.data);
//distinguish between both xml types
if(xml.song != undefined){//if song node exists, then it is music xml
_XML.appendChild(xml.song);
dispatchEvent(new Event(XMLLoader.MUSIC_XML_LOADED));
}
if(xml.video != undefined){//if video node exists, then it is video xml
_XML.appendChild(xml.video);
dispatchEvent(new Event(XMLLoader.VIDEO_XML_LOADED));
}
_XMLFilesPointer++;
if(_XMLFilesPointer < _XMLFiles.length)
loadXML();
}
public function get XMLData():XML {
return _XML;
}
}
}