首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex4操作xml资料实例

2012-08-21 
flex4操作xml文件实例s:Application xmlns:fxhttp://ns.adobe.com/mxml/2009 ?????xmlns:slibrary:/

flex4操作xml文件实例
<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/halo" minWidth="1024" minHeight="768"
????? creationComplete="init();">
<fx:Script>
?? <![CDATA[
??? import mx.rpc.events.ResultEvent;
??? [Bindable]
??? private var selfXML:XML = new XML();??? //外部xml文件对象
??? private var selfXMLList:XMLList = new XMLList();
???
??? private function init():void{
???? areaNoteID.text = "一、访问外部xml文件的方式:\n" +
????? "采用httpService的方式,返回值强制转化为XML类对象,用此对象操纵xml节点。\n\n\n" +
????? "二、解析外部xml文件的原则:\n" +
????? "?? 1、采用flex的XML类;\n" +
????? "?? 2、由类对象发起看,不用根节点,从二级节点开始逐层引用(必须是逐层的)。例如:\n" +
????? "??????? obj.[二级节点].[...].[基层节点],根据不同情况,每个节点都可看成一个数组对象。\n" +
????? "??????? 就像上面的selfXML.b.c.d[0]一样。\n" +
????? "?? 3、必须是字符串形式的数据才能赋值给节点。\n\n\n" +
????? "三、解析自定义动态xml文件的原则:\n" +
????? "?? 1、<><a></a></>中a是根节点,所以操纵xml时从b开始。\n\n\n" +
????? "四、XML类和XMLList类的区别:\n" +
????? "?? 1、XML类直接操作xml文件,并获取里面的数据。\n" +
????? "?? 2、XMLList类可以动态定义xml文件,可绑定到DataGrid等组件上。";
??? }
???
??? private function mySend01():void{??? //访问外部xml文件
???? service01ID.url = "testXML.xml";
???? service01ID.method = "get";
???? service01ID.resultFormat = "e4x";??? //resultFormat的值必须是小写的
???? service01ID.send();
??? }
???
??? private function getXML01(evtSelect:ResultEvent):void{
???? selfXML = service01ID.lastResult as XML;
???? textArea01ID.text = selfXML;
???? res01ID.text = "selfXML.b.c.d[0] = " + selfXML.b.c.d[0] + "\n" +
??????????????? "selfXML.child('b')[1].c.d[1] = " + selfXML.child("b")[1].c.d[1] + "\n" +
????????? "selfXML.d = " + selfXML.child("d")[0];
??? }
???
??? private function creatSelfXML():void{
???? selfXMLList =
??????? <>
?????????????????????? <a>
????????????????????????? <b>
??????????????? <c>
?????????????????? <d>刘德华</d>
?????????????????? <d>张学友</d>
???????????????????????????? </c>
????????????????????????? </b>
?????????????????????? </a>
??????? <a>
???????????? <b>
??????????????? <c>
?????????????????? <d>郑源</d>
?????????????????? <d>香港</d>
???????????????????????????? </c>
??????????????? <c>
?????????????????? <d>威尼斯</d>
?????????????????? <d>休斯顿</d>
???????????????????????????? </c>
???????????????????????? </b>
?????????????????????? </a>
??????????????????? </>;
???? textArea02ID.text = selfXMLList;
???? res02ID.text = "selfXMLList.b.c.d[2] = " + selfXMLList.b.c.d[2] + "\n" +
??????????????? "selfXMLList.child('b').c.d[3] = " + selfXMLList.child("b").c.d[3];
??? }
???
??? private function changelXML():void{
???? var num:Number = 1;
???? selfXMLList.b.c.d[2] = selfXML.b.c.d[0];
???? textArea02ID.text = selfXMLList;
??? }
???
??? private function allReplace():void{??? //XML全部转化为XMLList
???? selfXMLList = <>{selfXML}</>;
???? textArea02ID.text = selfXMLList;
???? trace(selfXML.length());
???? trace(selfXML.b.c[0]);
???? trace(selfXMLList.length());
???? trace(selfXMLList.b.c[0]);
??? }
???
?? ]]>
</fx:Script>

<mx:Button id="butWaiID" x="200" y="10" label="获得外部xml文件" click="mySend01();"/>
<mx:TextArea id="textArea01ID" x="100" y="50" width="300" height="500"/>
<mx:TextArea id="res01ID" x="450" y="50" width="200" height="100"/>

<mx:Button id="butDongID" x="1000" y="10" label="获得自定义动态xml文件" click="creatSelfXML();"/>
<mx:TextArea id="textArea02ID" x="1000" y="50" width="300" height="500"/>
<mx:TextArea id="res02ID" x="700" y="50" width="200" height="100"/>
<mx:Button id="changelID" x="1150" y="10" label="改变动态xml值" click="changelXML()"/>

<mx:Button id="allReplaceID" x="1250" y="10" label="全部转化" click="allReplace();"/>

<mx:TextArea id="areaNoteID" x="450" y="155" width="500" height="400"/>


<fx:Declarations>
?? <mx:HTTPService id="service01ID" useProxy="false" result="getXML01(event)"/>
</fx:Declarations>

</s:Application>

2、与主程序同目录的外部xml文件:testXML.xml

<?xml version="1.0" encoding="UTF-8" ?>
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="serverTemplate.xsd">
??? <b>
??????? <c>
??????????? <d>
????????????? 节点1
??????????? </d>
??????????? <d>
????????????? 节点2
??????????? </d>
??????????? <d>
????????????? 节点3
??????????? </d>
??????????? <d>
????????????? 节点4
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点5
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点6
??????????? </d>
??????? </c>
??? </b>
??? <b>
??????? <c>
??????????? <d>
????????????? 节点7
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点8
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点9
??????????? </d>
??????? </c>
??? </b>
??? <b>
??????? <c>
??????????? <d>
????????????? 节点10
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点11
??????????? </d>
??????? </c>
??????? <c>
??????????? <d>
????????????? 节点12
??????????? </d>
??????? </c>
??? </b>
</a>

热点排行