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

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

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

使用方法:

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

xml文件:

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

类文件:

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

留言

2006-07-06
次访问