Load Multiple XML Files


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package{
  2. //net
  3. import flash.net.URLLoader;
  4. import flash.net.URLRequest;
  5. //events
  6. import flash.events.Event;
  7. import flash.events.EventDispatcher;
  8.  
  9. public class XMLLoader extends EventDispatcher{
  10.  
  11. private var _URLLoader:URLLoader;
  12. private var _URLRequest:URLRequest;
  13. private var _XMLFiles:Array;
  14. private var _XML:XML;
  15. private var _XMLFilesPointer:int = 0;
  16. //private var _XMLPath:String = "xml/vday-music.xml";
  17.  
  18. //Custom Dispatchers
  19. public static var MUSIC_XML_LOADED:String = "music_xml_loaded";
  20. public static var VIDEO_XML_LOADED:String = "video_xml_loaded";
  21.  
  22. public function XMLLoader():void{
  23. _XMLFiles = new Array('xml/vday-music.xml','xml/vday-video.xml');
  24. _XML = new XML("<playlist></playlist>");
  25. }//end of constructor
  26.  
  27. public function loadXML():void{
  28. _URLLoader = new URLLoader();
  29. _URLRequest = new URLRequest(_XMLFiles[_XMLFilesPointer]);
  30.  
  31. _URLLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);
  32. _URLLoader.load(_URLRequest);
  33. }
  34.  
  35.  
  36. //Event Handlers
  37. private function xmlLoadComplete(e:Event):void{
  38. _URLLoader.removeEventListener(Event.COMPLETE, xmlLoadComplete);
  39.  
  40. var xml = XML(e.target.data);
  41. //distinguish between both xml types
  42. if(xml.song != undefined){//if song node exists, then it is music xml
  43. _XML.appendChild(xml.song);
  44. dispatchEvent(new Event(XMLLoader.MUSIC_XML_LOADED));
  45. }
  46. if(xml.video != undefined){//if video node exists, then it is video xml
  47. _XML.appendChild(xml.video);
  48. dispatchEvent(new Event(XMLLoader.VIDEO_XML_LOADED));
  49. }
  50. _XMLFilesPointer++;
  51. if(_XMLFilesPointer < _XMLFiles.length)
  52. loadXML();
  53. }
  54.  
  55. public function get XMLData():XML {
  56. return _XML;
  57. }
  58.  
  59. }
  60. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.