Flex 加载外部XML
方法一:XML类加载xml
<mx:XML id="xmlF" source="student.xml"/>
<mx:Panel x="0" y="200" title="XML类加载xml">
<mx:DataGrid dataProvider="{xmlF.student}">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name" />
<mx:DataGridColumn headerText="年龄" dataField="age" />
<mx:DataGridColumn headerText="性别" dataField="sex" />
</mx:columns>
</mx:DataGrid>
</mx:Panel>
方法二:Model类加载xml
把一中的<mx:XML id="xmlF" source="student.xml"/>
换成<mx:Model id="xmlF" source="student.xml"/>即可。
方法三:使用URLLoader
<mx:Script>
<![CDATA[
[Bindable]
private var loader:URLLoader=new URLLoader();
[Bindable]
private var xml:XML=new XML();
private function init():void{
loader.load(new URLRequest("student.xml"));
loader.addEventListener(Event.COMPLETE,onComplete);
}
private function onComplete(evt:Event):void{
xml=XML(loader.data);
}
]]>
</mx:Script>
<mx:DataGrid x="100" y="200" dataProvider="{xml.children()}">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name" />
<mx:DataGridColumn headerText="年龄" dataField="age" />
<mx:DataGridColumn headerText="性别" dataField="sex" />
</mx:columns>
</mx:DataGrid>
方法四:HTTPService类加载
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12" layout="absolute" creationComplete="xmlData.send()">
<mx:HTTPService id="xmlData" url="student.xml" result="ResultHandle(event)"/>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var ArrayXML:ArrayCollection;
[Bindable]
public var i:int=0;
/**加载后调用该函数*/
public function ResultHandle(event:ResultEvent):void
{
ArrayXML=event.result.students.student;
}
]]>
</mx:Script>
<mx:DataGrid x="92" y="348" width="390" dataProvider="{ArrayXML}">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="年龄" dataField="age"/>
<mx:DataGridColumn headerText="性别" dataField="sex"/>
<mx:DataGridColumn headerText="年级" dataField="cla"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>