Flex:ArrayCollection转xml形式的实例

本文展示了一种将ArrayCollection数据结构转换为XML格式的方法。通过定义ArrayCollection并使用自定义函数arrToXml,可以将每条记录转换为XML节点,并附加到根元素下。此外,还提供了一个示例应用程序,演示了如何在Flex环境中实现这一过程。

ArrayCollection转成xml类型示例

下面模拟一组数据exams,将其转化为xml形式.详细代码见下:

Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"fontSize="12"creationComplete="init()">
  3. <mx:Stylesource="style/Smoke.css"/>
  4. <mx:Script>
  5. <![CDATA[
  6. importmx.collections.ArrayCollection;
  7. [Bindable]privatevarshijxml:XML;
  8. privatefunctioninit():void{
  9. shijxml=arrToXml(exams);
  10. }
  11. [Bindable]
  12. publicvarexams:ArrayCollection=newArrayCollection(
  13. [{sjtitle:"数据结构第一章",kemu:"数据结构",shijID:"sj000",num:"30"},
  14. {sjtitle:"数据结构第二章",kemu:"数据结构",shijID:"sj001",num:"30"},
  15. {sjtitle:"组成原理第一章",kemu:"组成原理",shijID:"zc000",num:"50"},
  16. {sjtitle:"组成原理第二章",kemu:"组成原理",shijID:"zc001",num:"30"}]);
  17. privatefunctionarrToXml(arr:ArrayCollection):XML{
  18. varroot:XML=newXML("<root/>");
  19. for(vari:int=0;i<arr.length;i++){
  20. varnode:XML=newXML("<node/>");
  21. node.sjtitle=arr[i].sjtitle;
  22. node.kemu=arr[i].kemu;
  23. node.shijID=arr[i].shijID;
  24. node.num=arr[i].num;
  25. root.appendChild(node);
  26. }
  27. returnroot;
  28. }
  29. privatefunctioncloseHandler(event:Event):void{
  30. show.text="试卷标题:"+ComboBox(event.target).selectedItem;
  31. vartemp:String=String(ComboBox(event.target).selectedItem);
  32. foreach(varxml:XMLinshijxml.node){
  33. if(xml.sjtitle==temp){
  34. show.text="考试科目:"+xml.kemu+"/n"+"试卷题数:"+xml.num+"/n"+
  35. "试卷编号:"+xml.shijID;
  36. }
  37. }
  38. }
  39. ]]>
  40. </mx:Script>
  41. <mx:Panelwidth="100%"height="100%"layout="absolute">
  42. <mx:DataGridheight="100%"width="50%"dataProvider="{exams}">
  43. </mx:DataGrid>
  44. <mx:ComboBoxy="79"close="closeHandler(event);"dataProvider="{shijxml.node.sjtitle}"right="88"></mx:ComboBox>
  45. <mx:Texty="111"id="show"width="185"height="126"right="50"/>
  46. </mx:Panel>
  47. </mx:Application>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值