Flex中加载外部XML

在Flex Builder中,如果要加载一个外部的XML是比较简单的,将绝对路径或则是相对路径写入就可以了。但是如果是一个web的工程,这个问题就显得比较复杂了,因为这个时候,你得不到文件的绝对和相对路径,在Flex3中,解决这个问题,有如下两个方法。

    【一】mx:Model的方式
      
     程序主文件:
     
 1 <? xml version="1.0" ?>
 2 <!--  Simple example to demonstrate the LinkBar control.  -->
 3 < mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml" >
 4      < mx:Script >
 5          <![CDATA[             
 6          ]]>
 7      </ mx:Script >
 8      < mx:Model  id ="catalogService"  source ="news.xml"   />  
 9      < mx:ArrayCollection  id ="myXC"  source ="{catalogService.news}" />  
10      < mx:Panel  title ="TestLinkBarForXML"  
11         height ="323"  width ="466"  horizontalAlign ="center"
12         paddingTop ="10"  paddingBottom ="10"  paddingLeft ="10"  paddingRight ="10" >
13    
14          < mx:Text  width ="100%"  
15             text ="一个LinkBar的测试" />
16
17          < mx:LinkBar  color ="#0000FF"  fontWeight ="bold"  dataProvider ="{myViewStack}" />         
18         
19          <!--  Define the ViewStack and the three child containers.  -->
20          < mx:ViewStack  id ="myViewStack"  borderStyle ="solid"  width ="100%"  height ="80%" >
21
22              < mx:Canvas  id ="show1"  backgroundColor ="#FFFFCC"  label ="显示一"  width ="100%"  height ="100%" >
23             
24              </ mx:Canvas >
25
26              < mx:Canvas  id ="show2"  backgroundColor ="#CCFFFF"  label ="显示二"  width ="100%"  height ="100%" >
27              < mx:DataGrid  dataProvider ="{myXC}" />
28              </ mx:Canvas >
29
30              < mx:Canvas  id ="show3"  backgroundColor ="#FFCCFF"  label ="显示三"  width ="100%"  height ="100%" >
31              </ mx:Canvas >
32          </ mx:ViewStack >
33         
34      </ mx:Panel >
35      < mx:XML  id ="myXML"  source ="news.xml"   />
36   
37        < mx:Text  id ="myText"   width ="292" />
38 </ mx:Application >
39
40

    news.xml文件
 1 <? xml version="1.0" encoding="utf-8" ?>
 2 < main >
 3    < news >
 4       < newsTitle > 1-1 </ newsTitle >
 5       < newsItem > 1-2 </ newsItem >
 6    </ news >
 7    < news >
 8       < newsTitle > 2-1 </ newsTitle >
 9       < newsItem > 2-2 </ newsItem >
10    </ news >
11 </ main >


运行后画面:
Flex中加载外部XML

Flex中加载外部XML


    【二】mx:HTTPService的方式

    主程序:
1 <? xml version="1.0" ?>  
2 < mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  initialize ="catalogService.send()" >  
3      < mx:HTTPService  id ="catalogService"  url ="catalog.xml"  resultFormat ="e4x" />  
4      < mx:XMLListCollection  id ="myXC"  source ="{catalogService.lastResult.product}" />  
5      < mx:Repeater  id ="r"  dataProvider ="{myXC}"  startingIndex ="1" >  
6          < mx:RadioButton  id ="Radio"  label ="{r.currentItem.name}" />  
7      </ mx:Repeater >  
8 </ mx:Application >
9

   xml文件:
 1 <? xml version="1.0" ?>  
 2 < products >  
 3    < product >  
 4      < name > Name </ name >  
 5      < price > Price </ price >  
 6      < freeship > Free Shipping? </ freeship >  
 7    </ product >  
 8    < product >  
 9      < name > Whirlygig </ name >  
10      < price > 5 </ price >  
11      < freeship > false </ freeship >  
12    </ product >  
13    < product >  
14      < name > Tilty Thingy </ name >  
15      < price > 15 </ price >  
16      < freeship > true </ freeship >  
17    </ product >  
18 < product >  
19      < name > Really Big Blocks </ name >  
20      < price > 25 </ price >  
21      < freeship > true </ freeship >  
22    </ product >  
23 </ products >

转载: http://www.blogjava.net/rainwindboys/archive/2008/08/29/225610.html

你可能感兴趣的:(Flex)