转至: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>