在Flex Builder中,如果要加載一個外部的XML是比較簡單的,將絕對路徑或則是相對路徑寫入就可以了。但是如果是一個web的工程,這個問題就顯得比較復雜了,因為這個時候,你得不到文件的絕對和相對路徑,在Flex3中,解決這個問題,有如下兩個方法。
【一】mx:Model的方式
程序主文件:
news.xml文件
【二】mx:HTTPService的方式
主程序:
xml文件:
參考:http://www.flashxm.com/?p=169
【一】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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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>

2

3

4

5

6

7

8

9

10

11

運行后畫面:
【二】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

2

3

4

5

6

7

8

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>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

參考:http://www.flashxm.com/?p=169