ArrayCollection转成xml类型示例
下面模拟一组数据exams,将其转化为xml形式.详细代码见下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"fontSize="12"creationComplete="init()">
- <mx:Stylesource="style/Smoke.css"/>
- <mx:Script>
- <![CDATA[
- importmx.collections.ArrayCollection;
- [Bindable]privatevarshijxml:XML;
- privatefunctioninit():void{
- shijxml=arrToXml(exams);
- }
- [Bindable]
- publicvarexams:ArrayCollection=newArrayCollection(
- [{sjtitle:"数据结构第一章",kemu:"数据结构",shijID:"sj000",num:"30"},
- {sjtitle:"数据结构第二章",kemu:"数据结构",shijID:"sj001",num:"30"},
- {sjtitle:"组成原理第一章",kemu:"组成原理",shijID:"zc000",num:"50"},
- {sjtitle:"组成原理第二章",kemu:"组成原理",shijID:"zc001",num:"30"}]);
- privatefunctionarrToXml(arr:ArrayCollection):XML{
- varroot:XML=newXML("<root/>");
- for(vari:int=0;i<arr.length;i++){
- varnode:XML=newXML("<node/>");
- node.sjtitle=arr[i].sjtitle;
- node.kemu=arr[i].kemu;
- node.shijID=arr[i].shijID;
- node.num=arr[i].num;
- root.appendChild(node);
- }
- returnroot;
- }
- privatefunctioncloseHandler(event:Event):void{
- show.text="试卷标题:"+ComboBox(event.target).selectedItem;
- vartemp:String=String(ComboBox(event.target).selectedItem);
- foreach(varxml:XMLinshijxml.node){
- if(xml.sjtitle==temp){
- show.text="考试科目:"+xml.kemu+"/n"+"试卷题数:"+xml.num+"/n"+
- "试卷编号:"+xml.shijID;
- }
- }
- }
- ]]>
- </mx:Script>
- <mx:Panelwidth="100%"height="100%"layout="absolute">
- <mx:DataGridheight="100%"width="50%"dataProvider="{exams}">
- </mx:DataGrid>
- <mx:ComboBoxy="79"close="closeHandler(event);"dataProvider="{shijxml.node.sjtitle}"right="88"></mx:ComboBox>
- <mx:Texty="111"id="show"width="185"height="126"right="50"/>
- </mx:Panel>
- </mx:Application>