用URLLoader读取本地txt,xml等格式文件

转至:http://xiaobolove334421.blog.163.com/blog/static/9977622008529103718165/

<?xml version="1.0" encoding="GB2312"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

 

    <mx:Script>

       <![CDATA[

           /**

            * By kelvin 2008-07-10

            * Email:xiaobolove334421@163.com

            * */

          

           private function readText():void

           {

              var loader:URLLoader=new URLLoader();

              var urlFilepath:URLRequest=new URLRequest("Missyou.txt");

              loader.addEventListener(Event.COMPLETE,loadTextComplete);

              loader.load(urlFilepath);

           }

           private function loadTextComplete(event:Event):void

           {

              txtContent.text=event.target.data.toString();

              trace(event.target.data);

           }

          

           private function readXml():void

           {

              var loader:URLLoader=new URLLoader();

              var urlFilepath:URLRequest=new URLRequest("Movie.xml");

              loader.addEventListener(Event.COMPLETE,loadXmlComplete);

              loader.load(urlFilepath);

           }

           private function loadXmlComplete(event:Event):void

           {

              var xmlTemp:XML=new XML(event.target.data);

              dg.dataProvider=xmlTemp.movie;

              trace(xmlTemp);

           }

       ]]>

    </mx:Script>

 

    <mx:Button x="10" y="10" label="Read txt" click="readText()"/>

    <mx:TextArea id="txtContent" x="10" y="40" width="287" height="271"/>

   

    <mx:Button x="330" y="10" label="Read xml" click="readXml()"/>

    <mx:DataGrid id="dg" x="330" y="41" width="340" height="270">

       <mx:columns>

           <mx:DataGridColumn headerText="name" dataField="name"/>

           <mx:DataGridColumn headerText="actor" dataField="actor"/>

           <mx:DataGridColumn headerText="show date" dataField="date"/>

       </mx:columns>

    </mx:DataGrid>

   

</mx:Application>

 

在mxml文件同级目录下建两个文件:

1. Missyou.txt文件:

God:
 I miss you.

2. Movie.xml文件:

<movies>
 <movie>
  <name>全民公敌</name>
  <actor>威尔·史密斯,查理兹·塞隆</actor>
  <date>2008-07-09</date>
 </movie>
 <movie>
  <name>通辑犯</name>
  <actor>安吉莉娜·茱莉</actor>
  <date>2008-07-01</date>
 </movie>
 <movie>
  <name>机器人总动员</name>
  <actor>robert</actor>
  <date>2008-07-30</date>
 </movie>
</movies>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值