---
title: "Flex里解析XML数据"
author: "zwh910621"
date: "2009-12-16T10:07:39Z"
tags: []
categories: ["Web前端"]
slug: "flex-xml"
draft: false
lastmod: "2010-03-03T17:15:13Z"
id: 549444
url: "https://javaheart88.iteye.com/blog/549444"
---

我加了附件，里面是整个工程FLEX+WEB，包括JAR包，只要你tomcat下有blazeds.war就能运行（这是做FLEX项目就基本的，如果不明白，可以看我的更一个博客《MyEclipse+Flex》），需要解析的XML文件也都在里面，希望给初学者带来点帮助，也做为我以后复习的一个好资料

 

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

些工程主要的学习点：

使用&lt;mx:HTTPService&gt;&lt;/mx:HTTPService&gt;解析XML

基本的&lt;mx:DataGrid&gt;格式

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

 

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

列表显示所有数据的MXML.

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;mx:Application xmlns:mx="[http://www.adobe.com/2006/mxml](http://www.adobe.com/2006/mxml)" layout="absolute"&gt;  
&lt;mx:Script&gt;  
   &lt;![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](mailto:xml.officePhone.@ext) );  
   }  
   ]]&gt;   
&lt;/mx:Script&gt;  
&lt;mx:XML id="employees" source="employeesE4X.xml"/&gt;  
&lt;mx:List dataProvider="{empOfficePhones}" creationComplete="dotQuery()" top="20" width="75%" horizontalCenter="0"/&gt;  
&lt;/mx:Application&gt;

 

 

列表显示指定标签的MXML

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;mx:Application xmlns:mx="[http://www.adobe.com/2006/mxml](http://www.adobe.com/2006/mxml)" layout="absolute"&gt;  
&lt;mx:Script&gt;  
   &lt;![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));  
   }  
   ]]&gt;   
&lt;/mx:Script&gt;  
&lt;mx:XML id="employees" source="employeesE4X.xml"/&gt;  
&lt;mx:List dataProvider="{empFullNames}" id="listCombining" creationComplete="combineQuery()" top="20" width="75%" horizontalCenter="0" editable="false"/&gt;  
&lt;/mx:Application&gt;

 

从XML文件中获取搜索结果的MXML

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;mx:Application xmlns:mx="[http://www.adobe.com/2006/mxml](http://www.adobe.com/2006/mxml)" layout="absolute"&gt;  
&lt;mx:Script&gt;  
   &lt;![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](mailto:xml.@dept));  
   }  
   ]]&gt;   
&lt;/mx:Script&gt;  
&lt;mx:XML id="employees" source="employeesE4X.xml"/&gt;  
&lt;mx:List dataProvider="{empDept}" creationComplete="searchQuery()" top="20" left="20" width="75%" horizontalCenter="0" editable="false"/&gt;  
&lt;/mx:Application&gt;

修改XML文件指定标签内容的MXML例子

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;mx:Application xmlns:mx="[http://www.adobe.com/2006/mxml](http://www.adobe.com/2006/mxml)" layout="absolute"&gt;  
&lt;mx:Script&gt;  
   &lt;![CDATA[   
   import mx.collections.ArrayCollection;  
    
   private function changeDepartment(event:MouseEvent):void {  
    employees.employee.(id==1).@dept = tiDept.text;  
   }  
   ]]&gt;   
&lt;/mx:Script&gt;  
&lt;mx:XML id="employees" source="employeesE4X.xml"/&gt;  
&lt;mx:Canvas width="100%" height="100%"&gt;  
   &lt;mx:Form top="20" left="20" width="75%" horizontalCenter="0" fontWeight="bold"&gt;  
    &lt;mx:FormItem label="firstName"&gt;  
     &lt;mx:TextInput text="{employees.employee.(id==1).firstName}" editable="false"/&gt;  
    &lt;/mx:FormItem&gt;  
    &lt;mx:FormItem label="lastName"&gt;  
     &lt;mx:TextInput text="{employees.employee.(id==1).lastName}" editable="false"/&gt;  
    &lt;/mx:FormItem&gt;  
    &lt;mx:FormItem id="formItemDept" label="Department"&gt;  
     &lt;mx:TextInput id="tiDept" text="{employees.employee.(id==1).@dept}" editable="true"/&gt;  
    &lt;/mx:FormItem&gt;  
    &lt;mx:Spacer height="20"/&gt;  
    &lt;mx:HBox horizontalAlign="center" width="{formItemDept.width}"&gt;  
     &lt;mx:Button width="200" click="changeDepartment(event)" label="Change Bob Smith's Dept"/&gt;  
    &lt;/mx:HBox&gt;  
   &lt;/mx:Form&gt;  
&lt;/mx:Canvas&gt;  
&lt;/mx:Application&gt;