【积累】Flex中加载外部XML

本文介绍在FlexBuilder中通过两种方式加载XML的方法:使用mx:Model直接加载本地XML文件,并展示如何利用加载的数据;通过mx:HTTPService从网络获取XML数据并解析使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在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>


运行后画面:



    【二】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.flashxm.com/?p=169
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值