Flex解析XML文件的例子

发布时间:2020-04-13编辑:脚本学堂
Flex解析XML文件的例子

xml文件:
 

复制代码 代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<statisticals>
 <statisticalList>
  <statistical value="changsha" label="长沙">
   <statistical value="00" label="全口径"></statistical>
   <statistical value="01" label="部署口径"></statistical>
   <statistical value="02" label="地市公司"></statistical>
   <statistical value="03" label="县公司"></statistical>
  </statistical>

 

  <statistical value="loudi" label="娄底">
   <statistical value="00" label="全口径"></statistical>
   <statistical value="01" label="部署口径"></statistical>
  </statistical>

  <statistical value="hengyang" label="衡阳">
   <statistical value="00" label="地市公司"></statistical>
   <statistical value="01" label="直供"></statistical>
   <statistical value="02" label="泵售结算"></statistical>
   <statistical value="03" label="县公司"></statistical>
  </statistical>
 </statisticalList>
</statisticals>

 

AS解析程序:
复制代码 代码如下:
package util
{
 import mx.collections.ArrayCollection;
 import mx.rpc.events.ResultEvent;
 import mx.rpc.http.HTTPService;

 

 public class StatisticalUtil
 {
  public static var statisticalArr:ArrayCollection=new ArrayCollection();
  public static var viewXMLList:XMLList=new XMLList();

  public function StatisticalUtil()
  {
  }

  public static function getStatistical():void{
   var hts:HTTPService = new HTTPService();
   hts.url="assets/data/statistical.xml";
   hts.resultFormat="e4x";
   hts.addEventListener(ResultEvent.RESULT,handler);
   hts.send();
  }
  private static function handler(e:ResultEvent):void{
   //statisticalArr=e.result as ArrayCollection;
   viewXMLList = e.result.statisticalList;

   var oList:Object=new Object();
   oList=viewXMLList[0].statistical;
   for(var i:int=0;i<oList.length();i++){
    //log.debug(menuObj[i].@title)
    var o:Object=new Object();
    o.value=oList[i].@value
    o.label=oList[i].@label
    o.children=new ArrayCollection()
    var childObj:Object=oList[i].statistical
    for(var j:int=0;j<childObj.length();j++){
     var o1:Object=new Object();
     o1.value=childObj[j].@value
     o1.label=childObj[j].@label
     o.children.addItem(o1)
    }
    statisticalArr.addItem(o)
   }
  }

  public static function statisticalSource4OrgNo(orgNo:String,arr:ArrayCollection):ArrayCollection{
   var resultArr:ArrayCollection=new ArrayCollection();
   for(var t:int=0;t<arr.length;t++){
    if(orgNo==arr[t].value){
     resultArr=arr[t].children as ArrayCollection;
    }
   }
   return resultArr;
  }

 

 }
}