首页 > 网站 > WEB开发 > 正文

Flex 中 xml 与 ArrayCollection 之间的相互转换

2024-04-27 15:12:40
字体:
来源:转载
供稿:网友
xml 于 ArrayCollection 之间的相互转换 <?xml version="1.0" encoding="utf-8"?> <s:application xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="application1_creationCompleteHandler()" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.events.FlexEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; PRivate var xml:XML; [Bindable] private var dgdp:ArrayCollection=new ArrayCollection; protected function application1_creationCompleteHandler():void { http.send(); } protected function http_resultHandler(event:ResultEvent):void { xml = event.result as XML; for each(var xml:XML in event.result..des){ var o:Object=new Object(); o.id=xml.@id; o.name=xml.@name; o.score=xml.@score; dgdp.addItem(o); } arrToXml(); } protected function http_faultHandler(event:FaultEvent):void { // TODO Auto-generated method stub } protected function arrToXml():void{ var root:XML=<root/>; for(var i:int=0;i<dgdp.length;i++){ var node:XML=new XML(<node/>); node.id=dgdp[i].id; node.name=dgdp[i].name; node.score=dgdp[i].score; root.appendChild(node); } Alert.show(root.toXMLString()); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <s:HTTPService id="http" method="POST" useProxy="false" url="data/data.xml" resultFormat="e4x" result="http_resultHandler(event)" fault="http_faultHandler(event)"/> </fx:Declarations> <s:DataGrid id="dg" dataProvider="{dgdp}" x="202" y="41" width="346" height="164" requestedRowCount="4"> <s:columns> <s:ArrayList> <s:GridColumn dataField="id" headerText="id"></s:GridColumn> <s:GridColumn dataField="name" headerText="name"></s:GridColumn> <s:GridColumn dataField="score" headerText="score"></s:GridColumn> </s:ArrayList> </s:columns> </s:DataGrid> </s:Application>

data.xml

<?xml version="1.0" encoding="UTF-8"?><data value="test01"> <data1 id="1" value="test01"> <id id="1000" value="test02" /> <name name="test1" value="test03" /> <des id="1000" name="test1" score="75" classes="1class" value="test04" /> </data1> <data2 id="2" value="test02"> <id id="1001" value="test06" /> <name name="test2" value="test07" /> <des id="1001" name="test2" score="85" classes="2class" value="test08" /> </data2></data>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表