Very basic but I am new to writing classes.
comments and suggestions for improvements and advances are always appreciated.
DMC_XML is an XML loader that automatically makes available the number of children within the XML file for you.
Ideal when using a loop to populate fields from xml files, as the number of children is readily available after the load.
NOTES:
There is also a static variable that tracks the number of xml instances that have been loaded.
I am thinking of expanding the class to chain together multiple XML loads.
USAGE:
simply define a new variable as an instance of DMC_XML
var info: DMC_XML = new DMC_XML (“map_info”);
you can then reference the _xmlLength property to use in a loop that parses your xml.
var infoL:Number = info.get_xmlLength()
for(i=0; i<=infoL; i++){
code
}
class DMC_XML extends XML {
//
//***** private variables *****
//
private static var _xmlInstances:Number = 0;
private var _xmlFileName:String;
private var _xmlFile:XML;
private var _xmlLength:Number;
private var _interval:Number;
//
//***** constructor *****
//
public function DMC_XML (arg1:String) {
_xmlInstances++;
trace ("xml file loading = " + arg1);
this._xmlFileName = arg1;
this._xmlFile = new XML ();
this._xmlFile.ignoreWhite = true;
this._xmlFile.load ("xml/" + arg1 + ".xml");
this._interval = setInterval (this, "monitorLoad", 10);
this._xmlFile.onLoad = function (success) {
if (success) {
trace (arg1 + " xml file loaded");
}
};
}
//
//***** static methods *****
//
static function get_xmlInstances ():Number {
return _xmlInstances;
}
//
//***** getters & setters *****
//
public function get_xmlFileName ():String {
return this._xmlFileName;
}
public function set_xmlFileName (arg1:String):Void {
this._xmlFileName = arg1;
}
public function get_xmlFile ():XML {
return this._xmlFile;
}
public function set_xmlFile (arg1:XML):Void {
this._xmlFile = arg1;
}
public function get_xmlLength ():Number {
return this._xmlLength;
}
public function set_xmlLength (arg1:Number):Void {
this._xmlLength = arg1;
}
//
//***** methods *****
//
public function countChildren ():Void {
trace ("counting children in " + get_xmlFileName ());
var file:XML = get_xmlFile ();
var activeNode:XMLNode = file.firstChild.firstChild;
var nextNode:XMLNode = activeNode.nextSibling;
var lastNode:XMLNode = file.firstChild.lastChild;
var counter:Number = 0;
do {
counter++;
activeNode = nextNode;
nextNode = activeNode.nextSibling;
} while (nextNode.nodeName != null);
set_xmlLength (counter);
trace ("zero based number of children in " + get_xmlFileName () + " = " + counter);
}
//-----------------------------------------------------
//
private function monitorLoad () {
var bLoad:Number = _xmlFile.getBytesLoaded ();
var bTotal:Number = _xmlFile.getBytesTotal ();
//trace (bLoad);
//trace (bTotal);
if (bLoad > 0 && bLoad == bTotal) {
countChildren ();
//trace (_xmlFileName + " monitorLoad interval cleared");
clearInterval (this._interval);
}
}
//-----------------------------------------------------
//
}