XmlToObject类

  • 本站文章除注明转载外,均为本站原创或者翻译。
  • 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商;
  • 本站部分原创和翻译文章提供markdown格式源码,欢迎使用文章源码进行转载;
  • 本博客采用 WPCMD 维护;
  • 本文标题:XmlToObject类
  • 本文链接:http://zengrong.net/post/213.htm

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

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

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

使用方法:

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

xml文件:

<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>

类文件:

/*-----------------------------------------------------------------------------------*/
// 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);
    }
}