我絕對相信,你是在您的Flash / Flex應用程序中使用外部數據。 這是一個很好的做法,將XML格式的信息。 大多數的項目,我的工作還可以
使用XML,在大部分時間裡,我有一個類,將數據轉換為JSON對象。 與此工作流程的問題是,解析器的邏輯始終是不同的,因為XML是不同
的。 這些天,我寫了一個類,解決了這個問題,每一個給定的XML文件直接轉換成一個JSON對象。

XML文件,我測試:

<xml>
    <settings>
        <color>#990022</color>
        <size>12</size>
        <effects>
            <blur>yes</blur>
            <glow>no</glow>
            <drop-shadow>no</drop-shadow>
        </effects>
    </settings>
    <images>
        <base-url>http://site.com/pics/upload</base-url>
        <img id="image_01">
            <label><![CDATA[label of image 01]]></label>
            <url><![CDATA[url of image 01]]></url>
        </img>
        <img id="image_02">
            <label><![CDATA[label of image 02]]></label>
            <url><![CDATA[url of image 02]]></url>
        </img>
        <img id="image_03">
            <label>
                <en><![CDATA[English version]]></en>
                <bg><![CDATA[Bulgarian version]]></bg>
                <it><![CDATA[Italian version]]></it>
            </label>
            <url><![CDATA[url of image 03]]></url>
        </img>
    </images>
    <siteURL>http://site.com</siteURL>
    <user>
        <id session="3ddfa331fd1393029">952</id>
    </user>
</xml>
]]>

其結果是:

{
  settings={
    color=#990022
    effects={
      glow=no
      blur=yes
      drop-shadow=no
    }
    size=12
  }
  user={
    id={
      session=3ddfa331fd1393029
      _content=952
    }
  }
  images={
    img=[
      [0]={
        url=url of image 01
        id=image_01
        label=label of image 01
      }
      [1]={
        url=url of image 02
        id=image_02
        label=label of image 02
      }
      [2]={
        url=url of image 03
        id=image_03
        label={
          bg=Bulgarian version
          en=English version
          it=Italian version
        }
      }
    ]
    base-url=http://site.com/pics/upload
  }
  siteURL=http://site.com
}

用法:

var data:Object = XML2JSON.parse(new XML(...xml string here...));

有兩件事情,你必須注意:

1。 當你有沒有兒童(即文本節點)的節點,但它的屬性。 例如:

<id session="3ddfa331fd1393029">952</id>

被轉換為:


{
   session=3ddfa331fd1393029
   _content=952
}

IE中的文本節點連接到_content物業。

2。 當你有一個節點,這應該是一個數組對象,但目前只有一個孩子。 在這種情況下,該腳本將不承認你的孩子,作為一個數組元素,而是將它作為屬性添加。 這就是為什麼你應該描述這些節點數組中的財產XML2JSON類。 例如:

<images>
    <img>
        <label>label of image</label>
    </img>
</image>

將被轉換為:

{
    images={
        img={
            label=label of image
        }
    }
}

馬克IMG節點為一個數組:

XML2JSON.arrays = ["img"];
XML2JSON.parse(new XML(...xml string here...));

其結果將是:

{
    images={
        img=[
            [0]={
                label=label of image
            }
        ]
    }
}

這裡是代碼的類:

package {
     
    public class XML2JSON {
         
        private static var _arrays:Array;
         
        public static function parse(node:*):Object {
            var obj:Object = {};
            var numOfChilds:int = node.children().length();
            for(var i:int = 0; i<numOfChilds; i++) {
                var childNode:* = node.children()[i];
                var childNodeName:String = childNode.name();
                var value:*;
                if(childNode.children().length() == 1 && childNode.children()[0].name() == null) {
                    if(childNode.attributes().length() > 0) {
                        value = {
                            _content: childNode.children()[0].toString()
                        };
                        var numOfAttributes:int = childNode.attributes().length();
                        for(var j:int=0; j<numOfAttributes; j++) {
                            value[childNode.attributes()[j].name().toString()] = childNode.attributes()[j];
                        }
                    } else {
                        value = childNode.children()[0].toString();
                    }
                } else {
                    value = parse(childNode);
                }
                if(obj[childNodeName]) {
                    if(getTypeof(obj[childNodeName]) == "array") {
                        obj[childNodeName].push(value);
                    } else {
                        obj[childNodeName] = [obj[childNodeName], value];
                    }
                } else if(isArray(childNodeName)) {
                    obj[childNodeName] = [value];
                } else {
                    obj[childNodeName] = value;
                }
            }
            numOfAttributes = node.attributes().length();           
            for(i=0; i<numOfAttributes; i++) {
                obj[node.attributes()[i].name().toString()] = node.attributes()[i];
            }
            if(numOfChilds == 0) {
                if(numOfAttributes == 0) {
                    obj = "";
                } else {
                    obj._content = "";
                }
            }
            return obj;
        }
        public static function get arrays():Array {
            if(!_arrays) {
                _arrays = [];
            }
            return _arrays;
        }
        public static function set arrays(a:Array):void {
            _arrays = a;
        }
        private static function isArray(nodeName:String):Boolean {
            var numOfArrays:int = _arrays ? _arrays.length : 0;
            for(var i:int=0; i<numOfArrays; i++) {
                if(nodeName == _arrays[i]) {
                    return true;
                }
            }
            return false;
        }
        private static function getTypeof(o:*):String {
            if(typeof(o) == "object") {
                if(o.length == null) {
                    return "object";
                } else if(typeof(o.length) == "number") {
                    return "array";
                } else {
                    return "object";
                }
            } else {
                return typeof(o);
            }
        }
         
    }
     
}

arrow
arrow
    全站熱搜

    聆六禽 發表在 痞客邦 留言(0) 人氣()