1 package net.systemeD.halcyon {
3 /** A class permitting you to load XML files containing 'include' elements (for example,
4 * <include file="cuisine.xml" />, which will be automatically replaced with the contents of the file.
8 * xmlLoader=new NestedXMLLoader();
9 * xmlLoader.addEventListener(Event.COMPLETE, onFeatureLoad);
10 * xmlLoader.load("root.xml");
12 * onFeatureLoad can then access the XML via event.target.xml.
15 import net.systemeD.halcyon.FileBank;
17 import flash.events.*;
19 public class NestedXMLLoader extends EventDispatcher {
20 public var xml:XML = null;
21 private var count:int;
23 public function NestedXMLLoader() {
26 public function load(url:String):void {
27 FileBank.getInstance().addFromFile(url, fileLoaded);
30 private function fileLoaded(fileBank:FileBank, filename:String):void {
32 xml = new XML(fileBank.getAsString(filename));
33 for each (var inc:XML in xml.descendants('include')) {
40 private function replaceXML(inc:XML):void {
41 var xmlLoader:NestedXMLLoader=new NestedXMLLoader();
42 var includeElement:XML=inc;
43 xmlLoader.addEventListener(Event.COMPLETE, function(event:Event):void {
44 includeElement.parent().replace(findChildIndex(includeElement),event.target.xml);
47 xmlLoader.load(inc.@file);
50 private function findChildIndex(child:XML):int {
52 for each (var sibling:XML in child.parent().children()) {
53 if (sibling==child) return i;
59 private function decreaseCount():void {
60 count--; if (count>0) return;
64 private function fireComplete():void {
65 var event:Event=new Event(Event.COMPLETE);