Flex解析XML数据

简单的Flex项目-----使用flex解析xml

些工程主要的学习点:

使用<mx:HTTPService></mx:HTTPService>解析XML

基本的<mx:DataGrid>格式
Mxml代码

1. <mx:DataGrid id="http_dataGird" dataProvider="{http_test.lastResult.blog.channel.item}在哪里取的数据" width="100%" height="50%">
2. <mx:columns>
3. <mx:DataGridColumn headerText="给表头起的名字" dataField="title对应的数据"/>
4. <mx:DataGridColumn headerText="作者" dataField="author"/>
5. <mx:DataGridColumn headerText="类型" dataField="category"/>
6. <mx:DataGridColumn headerText="连接" dataField="link"/>
7. </mx:columns>
8. </mx:DataGrid>

<mx:DataGrid id="http_dataGird" dataProvider="{http_test.lastResult.blog.channel.item}在哪里取的数据" width="100%" height="50%">
<mx:columns>
<mx:DataGridColumn headerText="给表头起的名字" dataField="title对应的数据"/>
<mx:DataGridColumn headerText="作者" dataField="author"/>
<mx:DataGridColumn headerText="类型" dataField="category"/>
<mx:DataGridColumn headerText="连接" dataField="link"/>
</mx:columns>
</mx:DataGrid>













最后给初学者的一点介意。新学一样新技术,要多做总结,多练,整理好代码。比如写博客,给别人带来了方便,也给以后自己复习带来了更简明的更清晰的代码例子







下面是我转载的一些XML取值的方法

employeesE4X.xml



<employees>
<employee dept="sales">
<id>1</id>
<firstName>Bob</firstName>
<lastName>Smith</lastName>
<officePhone ext="234">(123)555-1111</officePhone>
</employee>
<employee dept="research">
<id>2</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<officePhone ext="1243">(123)555-2222</officePhone>
</employee>
<employee dept="finance">
<id>3</id>
<firstName>John</firstName>
<lastName>Smith</lastName>
<officePhone ext="9002">(123)555-7777</officePhone>
</employee>
<employee dept="sales">
<id>4</id>
<firstName>Jane</firstName>
<lastName>Jones</lastName>
<officePhone ext="6211">(123)123-1234</officePhone>
</employee>
<employee dept="finance">
<id>5</id>
<firstName>Art</firstName>
<lastName>DIsgreat</lastName>
<officePhone ext="3465">(123)777-1212</officePhone>
</employee>
<employee dept="sales">
<id>6</id>
<firstName>Brad</firstName>
<lastName>Notnails</lastName>
<officePhone ext="4325">(123)765-9876</officePhone>
</employee>
</employees>

列表显示所有数据的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;

[Bindable]
private var empOfficePhones:ArrayCollection = new ArrayCollection;

private function dotQuery():void{
for each(var xml:XML in employees.employee)
empOfficePhones.addItem(xml.firstName + ": " + xml.officePhone + " ext " + xml.officePhone.@ext );
}
]]>
</mx:Script>
<mx:XML id="employees" source="employeesE4X.xml"/>
<mx:List dataProvider="{empOfficePhones}" creationComplete="dotQuery()" top="20" width="75%" horizontalCenter="0"/>
</mx:Application>





列表显示指定标签的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.XMLListCollection;
import mx.collections.ArrayCollection;

[Bindable]
private var empFullNames:ArrayCollection = new ArrayCollection;

private function combineQuery():void{
for each(var xml:XML in employees.employee)
empFullNames.addItem(xml.firstName + " " + mxl.lastName));
}
]]>
</mx:Script>
<mx:XML id="employees" source="employeesE4X.xml"/>
<mx:List dataProvider="{empFullNames}" id="listCombining" creationComplete="combineQuery()" top="20" width="75%" horizontalCenter="0" editable="false"/>
</mx:Application>



从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;

[Bindable]
private var empDept:ArrayCollection = new ArrayCollection;

private function searchQuery():void{
for each(var xml:XML in employees.employee.(@dept == "finance"))
empDept.addItem(xml.firstName + " " + xml.lastName + "'s Department is " + xml.@dept);
}
]]>
</mx:Script>
<mx:XML id="employees" source="employeesE4X.xml"/>
<mx:List dataProvider="{empDept}" creationComplete="searchQuery()" top="20" left="20" width="75%" horizontalCenter="0" editable="false"/>
</mx:Application>

修改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;

private function changeDepartment(event:MouseEvent):void {
employees.employee.(id==1).@dept = tiDept.text;
}
]]>
</mx:Script>
<mx:XML id="employees" source="employeesE4X.xml"/>
<mx:Canvas width="100%" height="100%">
<mx:Form top="20" left="20" width="75%" horizontalCenter="0" fontWeight="bold">
<mx:FormItem label="firstName">
<mx:TextInput text="{employees.employee.(id==1).firstName}" editable="false"/>
</mx:FormItem>
<mx:FormItem label="lastName">
<mx:TextInput text="{employees.employee.(id==1).lastName}" editable="false"/>
</mx:FormItem>
<mx:FormItem id="formItemDept" label="Department">
<mx:TextInput id="tiDept" text="{employees.employee.(id==1).@dept}" editable="true"/>
</mx:FormItem>
<mx:Spacer height="20"/>
<mx:HBox horizontalAlign="center" width="{formItemDept.width}">
<mx:Button width="200" click="changeDepartment(event)" label="Change Bob Smith's Dept"/>
</mx:HBox>
</mx:Form>
</mx:Canvas>
</mx:Application>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值