在Flex3中处理XML

在Flex3中处理XML文件有两种方法,即:

         1. 使用Model标签;
         2. 使用HTTPService标签;

使用Model标签时,可以直接将XML数据输入在两个Model标签之间,也可以将XML文件的路径赋给Mode标签的source属性,具体代码如下:

         <mx:Application>
               <mx:Model id="model">
                    <>....<>
                    ...xml数据....
                    <>....<>
               </mx:Model>
         </mx:Application>
        
        或者
        
        <mx:Application>
               <mx:Model id="model" source="xml文件路径" />
         </mx:Application>

如上代码,xml文件加载后,要处理加载后的数据,还需要处理Application的creationComplete事件,及给creationComplete事件增加一个处理函数,如:xmlHandle(obj:Object):void,次函数必接受一个Object类型的参数,修改上面的代码如下:

        <mx:Application creationComplete="xmlHandle(model)">
               <mx:Script>
                     <!CDATA[
                             private function xmlHandle(obj:Object):void
                             {
                                   trace(obj.节点....);
                             }
                     ]]>
               </mx:Scirpt>
               <mx:Model id="model" source="xml文件路径" />
         </mx:Application>

使用HTTPService时,HTTPService会调用send()方法将xml文件中的数据发送给SWF文件,当SWF文件接受全部数据后,HTTPService会产生一个result事件,此事件为ResultEvent的一个实例,xml文件的数据会保存在ResultEvent实例的result属性中。HTTPService的lastResult属性也会保存有xml文件的数据,所以要处理加载进来的xml数据,就需要侦听result事件。加载进来的数据会保存在一个ArrayCollection数组中。具体修改代码如下:

         <mx:Application creationComplete="model.send()">
               <mx:Script>
                     <!CDATA[
                             import mx.collection.ArrayCollection;
                             import mx.rpc.events.ResultEvent;

                             
                             private var xmlData:ArrayCollection;
                             private function xmlHandle(evt:ResultEvent):void
                             {
                                   xmlData = evt.result.节点.....
                             }
                     ]]>
               </mx:Scirpt>
               <mx:HTTPService id="model" url="xml文件路径" result="xmlHandle(event)" />
         </mx:Application>

如果在加载xml数据是出错,可以监听HTTPService的fault事件。具体修改代码如下:

                <mx:Script>
                     <!CDATA[
                             import mx.collection.ArrayCollection;
                             import mx.rpc.events.ResultEvent;
                             import mx.rpc.events.FaultEvent;

                             private var xmlData:ArrayCollection;
                             private function xmlHandle(evt:ResultEvent):void
                             {
                                   xmlData = evt.result.节点.....
                             }

                              private function faultHandle(evt:FaultEvent):void
                             {
                                   trace("加载xml出错");
                             }

                     ]]>
               </mx:Scirpt>
               <mx:HTTPService id="model" url="xml文件路径" result="xmlHandle(event)" fault="faultHandle(event)"/>


你可能感兴趣的:(xml)