Flex中HTTPService对象的e4x和ArrayCollection两种数据处理(备忘)

Posted by 蒋波涛 17 November,2009 Views (0)Comment

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.collections.XMLListCollection;
            import VOB.Books;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;

            //绑定BooksArray
            [Bindable]
            private var BooksArrayCol:ArrayCollection=new ArrayCollection();
            public function showData(event:ResultEvent):void{
                //直接返回为ArrayCollection对象
                var arrayCol:ArrayCollection=event.result.rss.channel.item;
                for each(var array:Object in arrayCol){
                    var book:Books=new Books();
                    book.link=array.link;
                    book.title=array.title;
                    BooksArrayCol.addItem(book);
                }
            }
            [Bindable]
            private var roomList:XMLListCollection;
            public function showData2(event:ResultEvent):void{
               //关键是下面的三句,如何处理e4x格式返回的XML数据
                var xmllist:XMLList = new XMLList(event.result as XML);
                roomList = new XMLListCollection(xmllist.children().child("item"));
                trace(xmllist.children().child("item"));
            }
            public function dateformat(item:Object, pubDate:DataGridColumn):String{
                trace(item.pubDate);;
                //return publishdate.format(item.pubDate);
                return item.pubDate;
            }
            public function error(event:FaultEvent):void{
            }           
        ]]>
    </mx:Script>
    <mx:HTTPService id="httpService" url="http://www.hmgis.cn/feed.asp" result="showData(event)" />
    <mx:HTTPService id="httpService2" url="http://www.allpointsblog.com/feeds/index.rss2" result="showData2(event)" fault="error(event)" resultFormat="e4x"/>
    <mx:DateFormatter id="publishdate" formatString="DD MM,YYYY"></mx:DateFormatter>
    <mx:DataGrid x="55" y="86" id="datagrid" dataProvider="{BooksArrayCol}">
        <mx:columns>
            <mx:DataGridColumn headerText="Link" dataField="link"/>
            <mx:DataGridColumn headerText="Title" dataField="title"/>
        </mx:columns>
    </mx:DataGrid>
    <mx:DataGrid x="300.5" y="10" id="datagrid2" dataProvider="{roomList}" width="480.5" height="240">
        <mx:columns>
            <mx:DataGridColumn headerText="标题" dataField="title"/>
            <mx:DataGridColumn headerText="作者" dataField="author"/>
            <mx:DataGridColumn headerText="PubDate" dataField="pubDate" labelFunction="dateformat"/>
        </mx:columns>
    </mx:DataGrid>   
    <mx:Button x="55" y="258" label="Button" click="httpService.send()"/>
    <mx:Button x="419" y="258" label="Button" id="btn2" click="httpService2.send()"/>
</mx:Application>

注意:DataGrid对于XML数据并不能自动计算出各列的内容,这一特点只在数据源为ArrayCollection时才有效。

                var xmllist:XMLList = new XMLList(event.result.channel.item);
                roomList = new XMLListCollection(xmllist);

Related Items

Categories : Flex Tags : Flex  
Comments
Leave a comment

Or, take a look at Archives and Categories

目录

存档