Flex : 利用HTTPService DataGrid从XML文件中加载数据

datagrid.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
     creationComplete
="srv.send()"><!-- 发送HTTPService请求-->
    
    
<mx:Script >
        
<![CDATA[
            import mx.collections.XMLListCollection;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
            [Bindable]
            private var users:XML;

          private  function resultUsers(event:ResultEvent):void{//处理结果
          users = XML(event.result);
          datagrid.dataProvider=users.children();//XMLList
          
          }
        
]]>
    
</mx:Script>
    
    
<mx:HTTPService url="XMLFile.xml" useProxy="false" id="srv" resultFormat="xml" result="resultUsers(event)"><!--处理结果函数-->
        
    
</mx:HTTPService>
        
<mx:Panel x="416" y="75" width="458" height="269" layout="absolute" id="panel" title="用户信息" fontSize="14" fontWeight="bold" fontFamily="Verdana" color="#BB8BDD" borderStyle="solid" borderThickness="3" borderColor="#0E0505" cornerRadius="20" themeColor="#A3C2D8" alpha="0.85" backgroundColor="#FFFFFF" backgroundAlpha="0.84">
                
<mx:DataGrid x="32" y="10" width="374" height="193" id="datagrid" enabled="true" fontSize="13" fontWeight="bold" textAlign="center" color="#526BBE" borderColor="#C0C8CC" themeColor="#A9B1B3" alpha="0.86" alternatingItemColors="[#E9E9E9, #EFF8F9]" borderStyle="inset">
                    
<mx:columns>
                        
<mx:DataGridColumn headerText="ID" dataField="ID"/>
                        
<mx:DataGridColumn headerText="姓名" dataField="Name"/>                       
                    
</mx:columns>
                
</mx:DataGrid>              
        
</mx:Panel>
</mx:Application>

XMLFile.xml

  <?xml version="1.0" encoding="utf-8" ?> 
  
<guestbook>
  
<guest>
  
<Name>25</Name> 
  
<ID>25</ID> 
  
</guest>
  
<guest>
  
<Name>24</Name> 
  
<ID>24</ID> 
  
</guest>
  
<guest>
  
<Name>23</Name> 
  
<ID>23</ID> 
  
</guest>
  
<guest>
  
<Name>22</Name> 
  
<ID>22</ID> 
  
</guest>
  
<guest>
  
<Name>21</Name> 
  
<ID>21</ID> 
  
</guest>
  
<guest>
  
<Name>20</Name> 
  
<ID>20</ID> 
  
</guest>
  
<guest>
  
<Name>19</Name> 
  
<ID>19</ID> 
  
</guest>
  
<guest>
  
<Name>18</Name> 
  
<ID>18</ID> 
  
</guest>
  
<guest>
  
<Name>17</Name> 
  
<ID>17</ID> 
  
</guest>
  
<guest>
  
<Name>16</Name> 
  
<ID>16</ID> 
  
</guest>
  
<guest>
  
<Name>15</Name> 
  
<ID>15</ID> 
  
</guest>
  
<guest>
  
<Name>14</Name> 
  
<ID>14</ID> 
  
</guest>
  
<guest>
  
<Name>10</Name> 
  
<ID>10</ID> 
  
</guest>
  
<guest>
  
<Name>9</Name> 
  
<ID>9</ID> 
  
</guest>
  
<guest>
  
<Name>8</Name> 
  
<ID>8</ID> 
  
</guest>
  
<guest>
  
<Name>7</Name> 
  
<ID>7</ID> 
  
</guest>
  
<guest>
  
<Name>4</Name> 
  
<ID>4</ID> 
  
</guest>
  
<guest>
  
<Name>3</Name> 
  
<ID>3</ID> 
  
</guest>
  
<guest>
  
<Name>1</Name> 
  
<ID>1</ID> 
  
</guest>
  
</guestbook>

或者直接绑定:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="productsRequest.send()">
    
<mx:HTTPService id="productsRequest" url="countXML.asp" />
    
<mx:DataGrid x="20" y="80" id="productGrid" width="400" dataProvider="{productsRequest.lastResult.guestbook.guest}" >
        
<mx:columns>
        
<mx:DataGridColumn headerText="Name" dataField="Name" />
        
<mx:DataGridColumn headerText="ID" dataField="ID" />
        
</mx:columns>
    
</mx:DataGrid>
</mx:Application>
原文地址:https://www.cnblogs.com/Fooo/p/1587988.html