flex4操作xml文件实例

阅读更多

本实例共两个文件:

1、主程序:test.mxml


      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"
      creationComplete="init();">

       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是根节点,所以操纵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 =
        <>
                      
                         
               
                   刘德华
                   张学友
                            

                         

                      

       
            
               
                   郑源
                   香港
                            

               
                   威尼斯
                   休斯顿
                            

                        

                      

                    ;
     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]);
    }
   
   ]]>

















  

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



   
       
           
              节点1
           

           
              节点2
           

           
              节点3
           

           
              节点4
           

       

       
           
              节点5
           

       

       
           
              节点6
           

       

   

   
       
           
              节点7
           

       

       
           
              节点8
           

       

       
           
              节点9
           

       

   

   
       
           
              节点10
           

       

       
           
              节点11
           

       

       
           
              节点12
           

       

   

你可能感兴趣的:(adobe,flex,xml)