Flex读取xml文件

主要有两个方法:

 

(1)通过HTTPService

(2)通过URLLoader

 

代码如下:

 

mxml代码

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">  
  3. <mx:Script>  
  4.    <![CDATA[  
  5.     import mx.collections.ArrayCollection;  
  6.     import mx.rpc.events.ResultEvent;  
  7.     import mx.rpc.http.HTTPService;  
  8.      
  9.     public const xmlUrl:String = "config.xml";  
  10.     [Bindable] private var colors1:ArrayCollection;  
  11.     [Bindable] private var colors2:XML;  
  12.      
  13.     private function init():void{  
  14.      //方法一:通过HTTPService  
  15.      var service:HTTPService = new HTTPService();  
  16.      service.url = xmlUrl;  
  17.      service.addEventListener(ResultEvent.RESULT, resultHandler);  
  18.      service.send();  
  19.       
  20.      //方法二:通过URLLoader  
  21.      var request:URLRequest = new URLRequest(xmlUrl);  
  22.      var loader:URLLoader = new URLLoader(request);  
  23.      loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);  
  24.     }  
  25.      
  26.     private function resultHandler(event:ResultEvent):void{  
  27.      colors1 = event.result.colors.color;  
  28.     }  
  29.      
  30.     private function loaderCompleteHandler(event:Event):void{  
  31.      colors2 = new XML(event.target.data);  
  32.     }  
  33.    ]]>  
  34. </mx:Script>  
  35. <mx:List x="100" y="150" dataProvider="{colors1}" labelField="name">  
  36. </mx:List>  
  37. <mx:List x="300" y="150" dataProvider="{colors2.color}" labelField="@name">  
  38. </mx:List>  
  39. </mx:Application>  
 

config.xml文件

 

Java代码   收藏代码
  1. <colors>  
  2. <color name="LightGrey" value="#D3D3D3"/>  
  3. <color name="Silver" value="#C0C0C0"/>  
  4. <color name="DarkGray" value="#A9A9A9"/>  
  5. <color name="Gray" value="#808080"/>  
  6. <color name="DimGray" value="#696969"/>  
  7. <color name="Black" value="#000000"/>  
  8. </colors>  

 

实例:

 

登录

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import mx.controls.Alert;  
  6.             import mx.rpc.events.ResultEvent;  
  7.             import flash.net.navigateToURL;  
  8.               
  9.             private function goLogin():void{  
  10.                 login.send();  
  11.             }  
  12.             private function resultHandler(event:ResultEvent):void{  
  13.                 var entry:String = event.result.users.a.toString();  
  14.                 if(entry == 'ok'){  
  15.           
  16.                 }else{  
  17.                     Alert.show('Username or Password is/are wrong !');  
  18.                 }                 
  19.             }  
  20.         ]]>  
  21.     </mx:Script>  
  22.     <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="flexlogin.php" result="resultHandler(event)">  
  23.         <mx:request xmlns="">  
  24.             <mx:username> {username.text} </mx:username>  
  25.             <mx:userpwd> {userpwd.text} </mx:userpwd>  
  26.         </mx:request>  
  27.     </mx:HTTPService>  
  28.       
  29.     <mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">  
  30.         <mx:TextInput x="93" y="51" id="username" fontSize="12"/>  
  31.         <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true"/>  
  32.         <mx:Button x="73" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>  
  33.         <mx:Label x="32" y="53" text="用户名:" fontSize="12"/>  
  34.         <mx:Label x="43" y="97" text="密码:" fontSize="12"/>  
  35.         <mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2"/>  
  36.         <mx:Label x="10" y="10" text="测试用 用户名 User 密码 123456" fontSize="12" width="243"/>  
  37.     </mx:Panel>  
  38.       
  39. </mx:Application>  

 

flexlogin.php

 

Php代码   收藏代码
  1. <?php       
  2. $return="";  
  3. if(isset($_POST[username]) && isset($_POST[userpwd])){  
  4.  if ("User"==$_POST[username] && "123456"==$_POST[userpwd])  
  5.     $return="ok";  
  6.  else  
  7.     $return="error";  
  8. }  
  9. $xml_return = '<users>';  
  10. $xml_return.= '<a>'.$return.'</a>';  
  11. $xml_return.= '</users>';  
  12. echo $xml_return;  
 

实例:

 

rss

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import flash.net.navigateToURL;  
  6.         ]]>  
  7.     </mx:Script>  
  8.     <!-- 利用flex获取并显示数据2 -->  
  9.     <mx:HTTPService id="feedRequest" url="flexXml.xml" useProxy="false"/>  
  10.       
  11.     <mx:Panel x="10" y="10" width="475" height="400" layout="absolute" title="{feedRequest.lastResult.rss.channel.title}" >  
  12.           
  13.         <mx:DataGrid x="10" y="10" id="dgPosts" width="400" dataProvider="{feedRequest.lastResult.rss.channel.item}">  
  14.             <mx:columns>  
  15.                 <mx:DataGridColumn headerText="Posts" dataField="title"/>  
  16.                 <mx:DataGridColumn headerText="Date" dataField="pubDate" width="150"/>  
  17.             </mx:columns>  
  18.         </mx:DataGrid>  
  19.           
  20.         <mx:LinkButton x="10" y="225" label="Read full part" click="navigateToURL(new URLRequest(dgPosts.selectedItem.link));"/>  
  21.         <mx:TextArea x="10" y="175" width="400" htmlText="{dgPosts.selectedItem.description}"/>  
  22.           
  23.     </mx:Panel>  
  24.       
  25. </mx:Application>  
 

flexXml.xml

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rss version="2.0">  
  3.     <!-- 该文件放到tomcat的root目录 -->  
  4.     <channel>  
  5.         <title>你好,flex获取并显示数据</title>  
  6.   
  7.         <item>  
  8.             <title>hello world</title>  
  9.             <description> this is a best exanple in flex,you can do it with me ,and may you hava a wanderful future </description>  
  10.             <link>http://hi.baidu.com/shiryu963</link>  
  11.             <guid>http://hi.baidu.com/shiryu963</guid>  
  12.             <category>general</category>  
  13.             <pubDate>2009.6.8 12:17:11</pubDate>  
  14.         </item>  
  15.   
  16.         <item>  
  17.             <title>hello kava</title>  
  18.             <description> best wishs to me,this is a best exanple in flex,you can do it with me ,and may you hava a wanderful future </description>  
  19.             <link>http://hi.baidu.com/shiryu963</link>  
  20.             <guid>gech.com</guid>  
  21.             <category>general</category>  
  22.             <pubDate>2009.8.8 6:20:11</pubDate>  
  23.         </item>  
  24.   
  25.         <item>  
  26.             <title>hello shiryu</title>  
  27.             <description> i hava a best wish to you,this is a best exanple in flex,you can do it with me ,and may you hava a wanderful future </description>  
  28.             <link>http://hi.baidu.com/shiryu963</link>  
  29.             <guid>hahahhahhahahah</guid>  
  30.             <category>improtant</category>  
  31.             <pubDate>2009.4.22 6:20:11</pubDate>  
  32.         </item>  
  33.     </channel>  
  34. </rss>  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值