FLEX 数组 转化成 xml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="AvitHandle()">
    
<mx:Script>
        
<![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.soap.WebService;
            import mx.rpc.events.ResultEvent;    
            import mx.controls.Alert;
            
            
public function AvitHandle():void
            {
                var web:WebService 
= new WebService();
                web.wsdl 
= "http://localhost:2250/flex%203.0%20asp.net%20test/WebServiceTest.asmx?wsdl";
                
//必须调用loadwsdl方法下载ws说明文件,否则即使调用webservice的方法也是没有反应的
                web.loadWSDL();
                
//添加成功调用侦听
                web.addEventListener(ResultEvent.RESULT,wsHandle);
                
//添加错误调用侦听
                web.addEventListener(FaultEvent.FAULT, fault);
                web.Readxml.send();
            }
            
            
//成功调用处理函数
            public function wsHandle(e:ResultEvent):void
            {
                
//ddd.text = e.result as String;    
                var menus:XML = XML(e.result as String);
                var results:XMLList 
= menus.children();
                                
                var array:Array 
= ElementToAttr(results);
                
                DataGrid1.dataProvider 
= array;
            }
            
            
/*将如下类型的xml转换成array,以便可以作为DataProvider给DataGrid,Tree等组件绑定数据        
            <?xml version="1.0" encoding="utf-8" ?> 
            <Items>
                <Item>
                <name>nam1</name> 
                  <grender>boy</grender> 
                  <from>guangxi</from> 
                  </Item>
                <Item>
                  <name>nam2</name> 
                  <grender>boy</grender>
                  <from>shanghai</from>
                </Item>
            </Items>
            
*/
            
public function ElementToAttr(results:XMLList):Array
            {
                var array:Array=new Array();
                for each(var child:XML in results){
                    var obj:Object=new Object();
                    
                    obj[child.nodeKind()]=child[child.nodeKind()];
                    for each(var ite:XML in child.children()){
                        obj[ite.name().toString()]=child[ite.name().toString()];
                    }
                    
                    /*
                    obj["name"]=child.name;
                    obj["grender"]=child.grender;
                    obj["from"]=child.from;
                    */
                    
                    array.push(obj);                    
               }
               return
 array;
            }
            
            
//错误处理函数
            public function fault(e:FaultEvent):void
            {
                Alert.show(e.fault.faultString,
'Error')
            }
        ]]
>
    
</mx:Script>
    
<mx:Label text="Label" id="ddd" fontSize="12"/>
    
<mx:DataGrid id="DataGrid1">
        
<mx:columns>
            
<mx:DataGridColumn headerText="姓名" dataField="name"/>
            
<mx:DataGridColumn headerText="性别" dataField="grender"/>
            
<mx:DataGridColumn headerText="籍贯" dataField="from"/>
        
</mx:columns>
    
</mx:DataGrid>
</mx:Application>

posted @ 2011-03-01 14:31  春哥也编程  阅读(529)  评论(0编辑  收藏  举报