Flex中读取外部本地XML文件,为DataGrid提供数据的示例:
MXML文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[0.4, 1.0]" backgroundGradientColors="[#9154DD, #6798C2]">
<mx:Model id="ShoppingCartList" source="myData/ShoppingCartList.xml" />
<mx:Panel layout="absolute" right="10" bottom="10" top="255" width="250" title="购物车">
<mx:DataGrid x="0" y="0" width="230" editable="false" dragEnabled="true" dataProvider="{ShoppingCartList.item}" height="162" bottom="40" top="0">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Price" dataField="price"/>
<mx:DataGridColumn headerText="Number" dataField="number"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="172" y="172" label="移除" fontSize="12" bottom="10" right="10"/>
</mx:Panel>
</mx:Application>
提供数据的ShoppingCartList.xml文件,保存在myData/ShoppingCartList.xml下,代码如下:
<ShoppingCartList>
<item>
<name>Apple</name>
<price>2.33</price>
<number>12</number>
</item>
<item>
<name>Computer</name>
<price>2043.3</price>
<number>1</number>
</item>
<item>
<name>TV</name>
<price>3000</price>
<number>2</number>
</item>
<item>
<name>Camera</name>
<price>2311.23</price>
<number>1</number>
</item>
<item>
<name>Keyboard</name>
<price>3.3</price>
<number>12</number>
</item>
</ShoppingCartList>