WebService返回xml,转换成Array,绑定DataGrid

dee.mxml
<?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>

WebService方法
    [WebMethod]
    
public string Readxml()
    {
        
string str;
        str 
= "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
        str 
+= "<Items>";

        
for (int i = 1; i < 10; i++)
        {
            str 
+= "<Item>";
            str 
+= "<name>nam"+ i +"</name>";
            str 
+= "<grender>boy" + i + "</grender>";
            str 
+= "<from>shanghai" + i + "</from>";
            str 
+= "</Item>";
        }

        str 
+= "</Items>";

        XmlDocument dcxml 
= new XmlDocument();
        dcxml.LoadXml(str);

        
return str;
    }
posted on 2008-06-28 17:35  一粒沙  阅读(3929)  评论(1编辑  收藏  举报