loadXML() Function

This function takes an xml file and creates an object of its contents making it accessible after loading

package {
	// required classes
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
 
	public class loadXML { 
 
		// supply path to xml file as only parameter
		private function loadXML(file:String = null) { 
			if (!file) { return; } 
 
			var _xmlFile:String = file;
			var images:Array = new Array();
			var url = new URLRequest(_xmlFile);
			var loader:URLLoader = new URLLoader(url);
			loader.addEventListener(Event.COMPLETE, xmlLoadComplete);
 
			function xmlLoadComplete(event:Event):void { 
				var val;
				var i:uint;
				var prop:String;
 
				// image data
				xmlList = new XMLList(xml.image);
				data = new Array();				
				for (i = 0; i < xmlList.length(); i++) {
					var temp:Object = new Object();
					temp['id'] = i;
					for (var a = 0; a < xmlList[i].attributes().length(); a++) { 
						prop = xmlList[i].attributes()[a].name();
						val = xmlList[i].attributes()[a];
						temp[prop] = val;
					}
					temp['text'] = xmlList[i];
					data.push(temp);
				}
				// execute command here
				trace(data.length);
 
			}// end xmlLoadComplete()
		}// end loadXML()
	}// end class
}// end package


You must be logged in to post a comment.