Flex里解析XML数据

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

 

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

些工程主要的学习点:

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

基本的<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>

 

你可能感兴趣的:(xml,MyEclipse,ext,Flex,Adobe)