ActionScript 3 加载多个XML文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 加载多个XML文件相关的知识,希望对你有一定的参考价值。

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

	}
}

以上是关于ActionScript 3 加载多个XML文件的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 如何加载最新的XML文件来解决缓存问题

未在加载函数之外保存的数组 - ActionScript 3 和 XML

ActionScript 3 加载xml

ActionScript 3 基本的外部XML加载器

ActionScript 3 加载xml

ActionScript 3 AS3:XML加载