加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex中Object, String, XML, ArrayCollection互转

发布时间:2020-12-15 01:13:48 所属栏目:百科 来源:网络整理
导读:? import mx.collections.ArrayCollection; import mx.rpc.xml.SimpleXMLEncoder; import mx.rpc.xml.SimpleXMLDecoder; import mx.utils.ObjectUtil; ? private function xmlToArrayCollection(xml:XML):ArrayCollection{??????????? ????? ??????????? var
?

import mx.collections.ArrayCollection;

import mx.rpc.xml.SimpleXMLEncoder;

import mx.rpc.xml.SimpleXMLDecoder;

import mx.utils.ObjectUtil;

? private function xmlToArrayCollection(xml:XML):ArrayCollection{??????????? ?????

??????????? var xmlDoc:XMLDocument = new XMLDocument(xml.toString());

?????????? var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);

??????????? var resultObj:Object = decoder.decodeXML(xmlDoc);

??????????? var ac:ArrayCollection =new ArrayCollection(Array(resultObj.root.list.source.item));

??????????? return ac;

}

private function objectToXML(obj:Object):XML {

??????????? var qName:QName = new QName(”root”);

??????????? var xmlDocument:XMLDocument = new XMLDocument();

??????????? var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);

??????????? var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj,qName,xmlDocument);

??????????? var xml:XML = new XML(xmlDocument.toString());

??????????? return xml;

}

private function objectToArrayCollection(obj:Object):ArrayCollection {

??????????? var ac:ArrayCollection =? new ArrayCollection(obj as Array);

??????????? return ac;

}

private function arrayCollectionToXML(ac:ArrayCollection):XML{

var xml:XML = objectToXML(ac);

return xml;

}

private function init():void{

? var arr:Array = new Array();

? arr.push({data:0,name:’deva’});

? arr.push({data:1,name:’raj’});

? var ac:ArrayCollection = new ArrayCollection(arr);

? var xml:XML = arrayCollectionToXML(ac);

var newAc:ArrayCollection = xmlToArrayCollection(xml);

trace(newAc[0][0].name);

}

private function objToStr(value:Object,indent:int =0,

???????????????????????????????????????? refs:Dictionary= null,

???????????????????????????????????????? namespaceURIs:Array = null,

???????????????????????????????????????? exclude:Array = null):String{

var str:String;

var refCount:int = 0;

if (value is Date)

??????????? {

??????????????? return value.toString();

??????????? }

??????????? else if (value is XMLNode)

???? ???????{

??????????????? return value.toString();

??????????? }

??????????? else if (value is Class)

??????????? {

??????????????? return “(” + getQualifiedClassName(value) + “)”;

??????????? }

??????????? else

??????????? {

??????????????? var classInfo:Object = ObjectUtil.getClassInfo(value,exclude,

??????????????????? { includeReadOnly: true,uris: namespaceURIs });

??????????????? var properties:Array = classInfo.properties;

??????????????? str = “(” + classInfo.name + “)”;

??????????????? if (refs == null)

??????????????????? refs = new Dictionary(true);

??????????????? var id:Object = refs[value];

??????????????? if (id != null)

??????????????? {

??????????????????? str += “#” + int(id);

??????????????????? return str;

??????????????? }

??????????????? if (value != null)

??????????????? {

??????????????????? str += “#” + refCount.toString();

??????????????????? refs[value] = refCount;

??????????????????? refCount++;

??????????????? }

??????????????? var isArray:Boolean = value is Array;

??????????????? var isDict:Boolean = value is Dictionary;

??????????????? var prop:*;

??????????????? indent += 2;

??????????????? for (var j:int = 0; j < properties.length; j++)

??????????????? {

??????????????????? str = newline(str,indent);

??????????????????? prop = properties[j];

??????????????????? if (isArray)

??????????????????????? str += “[";

??????????????????? else if (isDict)

??????????????????????? str += "{";

??????????????????? if (isDict)

??????????????????? {

??????????????????????? str += objToStr(prop,indent,refs,

??????????????????????????????????????????????? namespaceURIs,exclude);

??????????????????? }

??????????????????? else

??????????????????? {

??????????????????????? str += prop.toString();

????? ??????????????}

??????????????????? if (isArray)

??????????????????????? str += "] “;

??????????????????? else if (isDict)

??????????????????????? str += “} = “;

??????????????????? else

??????????????????????? str += ” = “;

??????????????????? try

?????? ?????????????{

??????????????????????? str += objToStr(value[prop],exclude);

??????????????????? }

??????????????????? catch(e:Error)

??????????????????? {

????????????????????? ??str += “?”;

??????????????????? }

??????????????? }

??????????????? indent -= 2;

??????????????? return str;

??????????? }

}

private static function newline(str:String,n:int = 0):String

?? {

?????? var result:String = str;

?????? result += “n”;

?????? for (var i:int = 0; i < n; i++)

?????? {

?????????? result += ” “;

?????? }

?????? return result;

?? }

/* General function for Conversion (thanks,Krystian Bień)

?public function xmlToArrayCollection(xml:XML):ArrayCollection{

??????????? var temp:String = ‘<items>’ + xml.toString() + ‘</items>’;

??????????? xml = XML(temp);

??????????? var xmlDoc:XMLDocument = new XMLDocument(xml.toString());

??????????? var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);

??????????? var resultObj:Object = decoder.decodeXML(xmlDoc);

??????????? var ac:ArrayCollection;

??????????? ac = new ArrayCollection();

??????????? ac.addItem(resultObj.items);

??????????? return ac;???????????????

??????? }*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读