XML类包含用于处理XML对象的工作方法和属性。 XML类(以及XMLList,命名空间和QName类)基于ECMAScript中定义的XML(E4X)规范(ECMA - 357第2版)标准,实现了强大的XML处理在。使用toXMLString()方法返回的XML对象的字符串表示不论XML对象具有简单内容还是复杂内容。
但是flex的xml操作还不支持xquery等简便、强大的查询操作,但是对于一般的xml结点读取、属性读取、结点查找、属性查找、结点遍历等还是完全可以胜任。
本文提供了一个xml操作的实用Demo,主要有下述3部分内容:
1)构建xml;
2)xml结点读取、属性读取、结点遍历等;
3)用户可以利用..先得到所有结点列表xmllist,然后遍历该xmllist查找任何元素、结点。
<?xml version="1.0" encoding="utf-8"?>
<!--xml 操作demo-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="800"
height="600" creationComplete="init()" fontSize="11">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
protected function btnQuery_clickHandler(event:MouseEvent):void
{
var xmllist:XMLList = employees..employee; //列举所有员工信息
txtOpreate.text="";
for each(var item:XML in xmllist){ //遍历员工信息,看是否有匹配的zip
if(item.address.zip==txtZip.text){
txtOpreate.text = item.toXMLString();
break;
}
}
}
]]>
</fx:Script>
<fx:Script>
<![CDATA[
private var employees:XML;
//
protected function init():void{
//构建XML
employees =
<employees>
<employee ssn="123-123-1234">
<name first="John" last="Doe"/>
<address>
<street>11 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>98765</zip>
</address>
</employee>
<employee ssn="789-789-7890">
<name first="Mary" last="Roe"/>
<address>
<street>99 Broad St.</street>
<city>Newton</city>
<state>MA</state>
<zip>01234</zip>
</address>
</employee>
</employees>;
//显示xml
XML.prettyIndent = 2; //设置xml缩进量
txtSrc.text = employees.toXMLString();
}
protected function btnShow_clickHandler(event:MouseEvent):void
{
var output:String = "";
//employees.employee[0].address.zip, 98765
output+="employees.employee[0].address.zip: "+employees.employee[0].address.zip+"\r\n\r\n";
//employees.employee[1].@ssn, 789-789-7890
output+="employees.employee[1].@ssn: "+employees.employee[1].@ssn+"\r\n\r\n";
//employees.employee.name, <name first="John" last="Doe"/>
//<name first="Mary" last="Roe"/>
output+="employees.employee.name: "+employees.employee.name+"\r\n\r\n";
//employees..zip[0], 98765
output+="employees..zip[0]: "+employees..zip[0]+"\r\n\r\n";
//employees..@ssn[1], 789-789-7890
output+="employees..@ssn[1]: "+employees..@ssn[1]+"\r\n\r\n";
//employees..name, <name first="John" last="Doe"/>
//<name first="Mary" last="Roe"/>
output+="employees..name: "+employees..name+"\r\n\r\n";
//employees.employee[0].address.*, <street>11 Main St.</street>
//<city>San Francisco</city>
//<state>CA</state>
//<zip>98765</zip>
output+="employees.employee[0].address.*: "+employees.employee[0].address.*+"\r\n";
txtOpreate.text = output;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button id="btnShow" click="btnShow_clickHandler(event)" label="展示XML操作信息" left="3" top="3"/>
<s:TextInput id="txtZip" top="3" width="70" right="80" toolTip="请输入员工的ZIP" text="01234"/>
<s:Button id="btnQuery" top="3" width="70" right="3" toolTip="查找" label="查找"
click="btnQuery_clickHandler(event)"/>
<mx:HDividedBox id="dividedbox" top="30" bottom="3" left="3" right="3">
<s:TextArea id="txtSrc" width="30%" height="100%"/>
<s:TextArea id="txtOpreate" width="70%" height="100%"/>
</mx:HDividedBox>
</s:Application>
本文转自:http://blog.csdn.net/chuangxin/archive/2010/10/24/5962437.aspx