Flex中操作XML(下)
系統
1849 0
?
四 在Flex中使用XML的例子
大的XML文檔用來顯示數據或者顯示列表的情況比較多,比如顯示數據時作為Datagrid的數據源,或者為List,Combobox,Tree等的顯示列表。
?
當我們使用List或者Combobox的時候,往往會把XML對象轉換為ArrayCollection對象,看下面的例子
外部Xml文件
view plaincopy to clipboardprint?
<books>?????????????????????
??? <book name="flex tutorial">?????????????????
??????? <price>30</price>?????????????
??????? <author>adobe</author>????????????
??? </book>?????????????????
??? <book name="air tutorial">??????????????????
??????? <price>40</price>?????????????
??????? <author>adobe</author>????????????
??? </book>?????????????????
??? <book name="java tutorial">?????????????????
??????? <price>50</price>?????????????
??????? <author>sun</author>??????????????
??? </book>?????????????????
</books>???????????????????
<books>?????
<book name="flex tutorial">????
?? <price>30</price>???
?? <author>adobe</author>???
</book>????
<book name="air tutorial">????
?? <price>40</price>???
?? <author>adobe</author>???
</book>????
<book name="java tutorial">????
?? <price>50</price>???
?? <author>sun</author>???
</book>????
</books>
Flex文件
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>??????????????????
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"??????????????????
layout="absolute"????????????????
creationComplete="init()">????????????????
<mx:Script>???????????????
<!--[CDATA[??????????????????
????? import mx.collections.ArrayCollection;??????????????????
????? import mx.rpc.events.ResultEvent;???????????????
????? //用于數據綁定的ArrayCollection????????????????
????? [Bindable]private var externalData:ArrayCollection = new ArrayCollection();?????????????????
??????????????????????
????? private function init():void {??????????????????
????????? //發送請求??????????????????
????????? myService.send();???????????????
????? }???????????????
??????????????????????????
????? private function resultHandler(event:ResultEvent):void {????????????????
????????? //取得Xml對象中book節點的集合?????????????????
????????? externalData = event.result.books.book;?????????????????
????? }//斷點處??????????????????
]]-->????????????????
</mx:Script>??????????????????
<!--創建Httpservice對象加載外部Xml-->?????????????????
<mx:HTTPService id="myService"???????????????
????? url="xmlFile.xml"???????????????
????? result="resultHandler(event)"/>??????????????????
<!--用于顯示的list-->??????????????????
<mx:List id="datalist" dataProvider="{externalData}" labelField="name"/>??????????????????????????
</mx:Application>??????????????
<?xml version="1.0" encoding="utf-8"?>????
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"????
layout="absolute"????
creationComplete="init()">????
<mx:Script>????
<!--[CDATA[????
????? import mx.collections.ArrayCollection;????
????? import mx.rpc.events.ResultEvent;????
????? //用于數據綁定的ArrayCollection????
????? [Bindable]private var externalData:ArrayCollection = new ArrayCollection(); ????
???? ????
????? private function init():void {????
????????? //發送請求????
????????? myService.send();????
????? }????
???????? ????
????? private function resultHandler(event:ResultEvent):void {????
????????? //取得Xml對象中book節點的集合????
????????? externalData = event.result.books.book;????
????? }//斷點處????
]]-->????
</mx:Script>????
<!--創建Httpservice對象加載外部Xml-->????
<mx:HTTPService id="myService"????
????? url="xmlFile.xml"????
????? result="resultHandler(event)"/>????
<!--用于顯示的list-->????
<mx:List id="datalist" dataProvider="{externalData}" labelField="name"/>???????? ????
</mx:Application>
將上面的代碼以debug模式執行,程序停在斷點處,在variables視圖中我們可以很清晰的看到程序HttpService對象在加載外部 XML后已經把它轉換成了ArrayCollection對象,如下圖。這樣就可以很輕松的將數據源綁定到顯示列表對象List中。
?
?
另外一種情況,當我們使用Tree組件的時候,需要在HTTPService對象中加上resultFormat="e4x"以XML的格式讀取進來而不要轉換為ArrayCollection。
外部XML文件
view plaincopy to clipboardprint?
<books>?????
<category name="RIA">???
??? <book name="flex tutorial" price="30" author="adobe">??
??? </book>?????
??? <book name="air tutorial" price="40" author="adobe">????
??? </book>?????
</category>?????????
<category name="Java">??????????
??? <book name="java tutorial" price="50" author="sun">?????
??? </book>?????
</category>?????????
</books>???????
<books>
<category name="RIA">
<book name="flex tutorial" price="30" author="adobe">
</book>
<book name="air tutorial" price="40" author="adobe">
</book>
</category>??
<category name="Java">??
<book name="java tutorial" price="50" author="sun">
</book>
</category>??
</books>
Flex文件
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>??????????????
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"??????????????
layout="absolute"????????????
creationComplete="myService.send()">??????????????
<mx:HTTPService id="myService"???????????
??? url="xmlFile.xml"?????????
??? resultFormat="e4x"/>???????
??????????????
<mx:XMLListCollection id="booktreesrc"?????????????
??? source="{myService.lastResult.category}"/>?????????
??????????????
<mx:Tree id="bookTree"?????????????
??? height="100%"?????????
??? dataProvider="{booktreesrc}"??????????
??? labelField="@name"/>???????
??????????????????????
</mx:Application>??????????
<?xml version="1.0" encoding="utf-8"?>???
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"???
layout="absolute"???
creationComplete="myService.send()">???
<mx:HTTPService id="myService"???
url="xmlFile.xml"??
resultFormat="e4x"/>??
???
<mx:XMLListCollection id="booktreesrc"???
source="{myService.lastResult.category}"/>??
???
<mx:Tree id="bookTree"???
height="100%"??
dataProvider="{booktreesrc}"??
labelField="@name"/>??
??????? ???
</mx:Application>
畫面顯示
Flex中操作XML(下)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元