刚开始学Flex,读取XML信息常用,所以记下来分享...
首先,HttpService必须正常使用.XML文件可以在http://localhost:端口/test.xml正常访问
其次,在MXML文件上,两个组件,Button,DataGrid
再次,相应的XML文件建立.
最后代码如下:
.mxml文件代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
public var returnCon:ArrayCollection=new ArrayCollection();
public function showGird():void
{
//将返回的object类型转换为ArrayCollection
returnCon=showDataGrid.lastResult.NodeFirst.Result as ArrayCollection;
dgShow.dataProvider=returnCon;
}
]]>
</mx:Script>
<mx:HTTPService id="showDataGrid" url="test.xml" result="showGird()">
</mx:HTTPService>
<mx:DataGrid x="176" y="133" id="dgShow">
<mx:columns>
<mx:DataGridColumn headerText="第一个值" dataField="NodeA"/>
<mx:DataGridColumn headerText="第二个值" dataField="NodeB"/>
<mx:DataGridColumn headerText="第三个值" dataField="NodeC"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="336" y="49" label="Show DataGrid" click="showDataGrid.send()"/>
</mx:Application>
test.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<NodeFirst>
<Result>
<NodeA>value1</NodeA>
<NodeB>value2</NodeB>
<NodeC>value3</NodeC>
</Result>
<Result>
<NodeA>value4</NodeA>
<NodeB>value5</NodeB>
<NodeC>value6</NodeC>
</Result>
</NodeFirst>
点击Button,查看DataGrid显示结果.