Return to Snippet

Revision: 23746
at February 13, 2010 20:26 by alvincrespo


Updated Code
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;
		}

	}
}

Revision: 23745
at February 12, 2010 02:34 by alvincrespo


Initial Code
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 _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{
			trace("XMLLoader Instantiated");
			_XMLFiles = new Array('xml/vday-music.xml','xml/vday-video.xml');
			loadXML();
		}//end of constructor
		
		public function loadXML():void{
			for(var i:int=0; i<_XMLFiles.length; i++){
				beginLoadingXML(_XMLFiles[i]);
			}
		}
		
		private function beginLoadingXML(pXMLPath:String):void{
			_URLLoader = new URLLoader();
			_URLLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);
			
			_URLRequest = new URLRequest(pXMLPath);
			_URLLoader.load(_URLRequest);		
		}
		
		//Event Handlers
		private function xmlLoadComplete(e:Event):void{
			_URLLoader.removeEventListener(Event.COMPLETE, xmlLoadComplete);
			_XML = new XML(e.target.data);
			//distinguish between both xml types
			if(_XML.song){//if song node exists, then it is music xml
				dispatchEvent(new Event(XMLLoader.MUSIC_XML_LOADED));
			}
			if(_XML.video){//if video node exists, then it is video xml
				dispatchEvent(new Event(XMLLoader.VIDEO_XML_LOADED));			
			}
		}
		
		public function get XMLDataAsXMLList():XMLList{
			return _XML.song;
		}
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
Load Multiple XML Files

Initial Tags
actionscript, xml, files, load

Initial Language
ActionScript 3