在Flex中将Array转换为XML
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();"> <mx:Script> <![CDATA[ import mx.rpc.xml.SimpleXMLEncoder; import mx.utils.ObjectUtil; [Bindable] public var arr:Array=[{id:"1",name:"aa"}, {id:"2",name:"bb"}, {id:"3",name:"cc"}]; private function init():void { textArea1.text = ObjectUtil.toString(arr);//Array数组直接显示 textArea2.text = objectToXML(arr).toXMLString();//Array数组通过XMLDocument转换 textArea3.text = arrToXml(arr).toString();//Array数组通过自定义转换 } 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 arrToXml(arr:Array):XML{ var root:XML = new XML("<root/>"); for(var i:int = 0;i<arr.length;i++){ var node:XML = new XML("<node/>"); node.id = arr[i].id; node.name= arr[i].name; root.appendChild(node); } return root; } ]]> </mx:Script> <mx:HDividedBox width="100%" height="100%"> <mx:TextArea id="textArea1" editable="false" width="100%" height="100%" /> <mx:TextArea id="textArea2" editable="false" width="100%" height="100%" /> <mx:TextArea id="textArea3" editable="false" width="100%" height="100%" /> </mx:HDividedBox> </mx:Application>
?