flash on 2011-7-22

by h_sakurai
♥0 | Line 31 | Modified 2011-07-22 14:12:08 | MIT License
play

ActionScript3 source code

/**
 * Copyright h_sakurai ( http://wonderfl.net/user/h_sakurai )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aZBN
 */


package {
    import flash.display.Sprite;
    import flash.text.TextField;
    
    public class Obj2XML extends Sprite {
        private var tf:TextField = new TextField();
        public function Obj2XML() {
            // write as3 code here..
            var a:Object = {h:"aaa",b:"ddd",c:["hoge",1,2]};
            tf.text = obj2xml("obj",a).toXMLString();
            addChild(tf);
        }
        
    }
}

function obj2xml(name:String, obj:Object):XML {

    var xml:XML = new XML("<"+name+"/>");
    if (obj is String || obj is Number || obj is int) {
        xml.appendChild(obj);
        return xml;
    }
    var i:String;

    if (obj is Array) {
//        var x2:XML = new XML("<Array/>");
        var x2:XML = xml;
        for (i in obj) {
            x2.appendChild(obj2xml("v", obj[i]));
        }
//        xml.appendChild(x2);
        return xml;
    }

    for (i in obj) {
        xml.appendChild(obj2xml(i, obj[i]));
    }
    return xml;
}