XmlToObject类

此类由 petex 制作,版权归原作者所有。

zrong(www.zengrong.net)修改,加入识别嵌套的xml文档的功能以及支持 type="object" 类型。

详情见 http://www.tweenpix.net/blog/index.php?2003/09/24/43-xmltoobject-as20-class

使用方法:

1import cn.mediasky.utils.XmlToObject;
2obj = new Object();
3obj.onMade = function() {
4  trace(this.main.url);
5}
6XmlToObject.loadData('test.xml', obj);

xml文件:

1<root>
2  <main type="object">
3    <url type="string">http://localhost/abc.php</url> 
4    <level type="number">3</level> 
5  </main>
6  <allowdomains type="array">'abc.net', 'abc.com', 'localhost'</allowdomains> 
7</root>

类文件:

  1/*-----------------------------------------------------------------------------------*/
  2// XmlToObject 2.0 (09.23.2003)
  3//  (c) petepx aka Francis Bourre (peterphonix@usa.net)
  4/*-----------------------------------------------------------------------------------*/
  5 
  6/*-----------------------------------------------------------------------------------*/
  7// XmlToObject 3.0 (07.06.2006)
  8//此类由petex制作,版权归原作者所有
  9//zrong(www.zengrong.net)修改,加入识别嵌套的xml文档的功能以及支持type="object"类型
 10//详情见http://www.tweenpix.net/blog/index.php?2003/09/24/43-xmltoobject-as20-class
 11//原来的版本以及离线帮助见utils.xml.XmlToObject
 12/*-----------------------------------------------------------------------------------*/
 13import mx.events.EventDispatcher;
 14 
 15/* // - Custom objects import example -
 16import com.robertpenner.Vector; */
 17 
 18class cn.mediasky.utils.XmlToObject {
 19 	
 20 	static var dispatchEvent:Function;
 21 	static var addEventListener:Function;
 22 	static var removeEventListener:Function;
 23 	
 24 	private static function _stripSpaces(sE:String) : String {
 25  		var sR : String = "";
 26          for (var x = 0; x < sE.length; x++) if (sE.charCodeAt(x) <> 32) sR += sE.charAt(x);
 27          return sR;
 28  	}
 29 	
 30 	private static function _ignoreSpaces(sE:String) : String {
 31  		var sR : String = "";
 32  		var canDel : Boolean = true;
 33          for (var x = 0; x < sE.length; x++)  {
 34                   if (sE.charCodeAt(x) == 34 || sE.charCodeAt(x) == 39) canDel = !canDel;
 35                   if (sE.charCodeAt(x) != 32)  {
 36                            sR += sE.charAt(x);
 37                    } else {
 38                            if (!canDel) sR += sE.charAt(x);
 39                    }
 40           }
 41          return sR;
 42  	}
 43 	
 44 	private static function _explode(sE:String) : Array {
 45  		var t:Array = _ignoreSpaces(sE).split(",");
 46  		var aR:Array = new Array();
 47  		for (var y=0; y<t.length; y++) t[y].charCodeAt(0) == 34 || t[y].charCodeAt(0) == 39 ? aR.push(t[y].substr(1,t[y].length-2)) : aR.push(Number(t[y]));
 48  		return aR;
 49  	}
 50 
 51 	private static function _made(xml:Array, rO:Object) : Object{
 52 		var o:Object = new Object();
 53 		var n:Number = xml.length;
 54		for (var x = 0; x < xml.length; x++) {
 55           	o[xml[x].nodeName] = {};
 56           	o[xml[x].nodeName]["data"] =xml[x].firstChild.nodeValue;
 57           	o[xml[x].nodeName]["varType"] = xml[x].attributes.type;
 58           	o[xml[x].nodeName]["nodeNum"] = x;	//保存当前xmlnode在父Node中的序号
 59   		}
 60   		for(var x in o) {
 61      		switch (o[x].varType) {
 62               	case "number" :
 63           		rO[x] = Number( _stripSpaces(o[x].data) );
 64           		break;
 65           		case "string" :
 66           		rO[x] = o[x].data;
 67           		break;
 68           		case "array" :
 69           		rO[x] = _explode(o[x].data);
 70           		break;
 71           		case "boolean" :
 72           		_stripSpaces(o[x].data) == "true" || Number( _stripSpaces(o[x].data) ) == 1 ? rO[x] = true : rO[x] = false;
 73           		break;
 74           		case "object" :
 75           		rO[x]= new Object();
 76           		//trace('xml['+o[x].nodeNum+'].childNodes:'+xml[o[x].nodeNum].childNodes);
 77           		rO[x] = _made(xml[o[x].nodeNum].childNodes, rO[x]);
 78           		break;
 79				/* // - Custom objects deserialization example -
 80				case "vector" :
 81				var t:Array = _explode(o[x].data);
 82				rO[x] = new Vector(t[0], t[1]);
 83				break; */
 84				case "default" :
 85				// do nothing, specify something if u want ...
 86				break;
 87           	}
 88           	n--;
 89  		}
 90  		return rO;
 91 	}
 92 	
 93 	public static function loadData(s:String, rO:Object) {
 94  		EventDispatcher.initialize(rO);
 95  		rO.addEventListener("onMade", rO);
 96  		var myXml = new XML();
 97  		myXml.ignoreWhite = true;
 98  		myXml.onLoad = function(success) {
 99   			if (success) {
100				rO = _made(myXml.firstChild.childNodes, rO);
101   				rO.dispatchEvent({type: "onMade", target: rO});
102    		} 
103   		}
104  		myXml.load(s);
105  	}
106}