flex中xml转化为array实例

本文共两个文件:translate.mxml 和 mapmarking.xml

1、translate.mxml

<?xml version="1.0" encoding="utf-8"?>
<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">

<fx:Script>
   <![CDATA[
    //xml translate array
    public function xmlToArr(xmlObj:XML):Array{
     var toArr:Array = new Array();
     for(var i:uint = 0; i < xmlObj.site.length(); i++){
      toArr.push({pid: xmlObj.site[i].id,
         province: xmlObj.site[i].province,
         city: xmlObj.site[i].city,
         updName: xmlObj.site[i].station,
         latitude: xmlObj.site[i].ypos[0],
         longitude: xmlObj.site[i].xpos,
         telephone: "110"
        });
     }
     return toArr;
    }
   
    //parse the array
    private function arrParse():void{
       var arr:Array = xmlToArr(xmlID);
     for(var i:int = 0; i < arr.length; i++){
        trace(arr[i].updName + "========" + arr[i].longitude);
     }
    }
   
   ]]>
</fx:Script>

<mx:Button id="butID" horizontalCenter="0" fontSize="18"
      label="xml translate array" click="arrParse();"/>

<fx:Declarations>
   <fx:XML id="xmlID" source="mapmarking.xml"/>
</fx:Declarations>

</s:Application>

 

 

2、mapmarking.xml

<?xml version="1.0" encoding="utf-8" ?>
<channel>
<site>
   <id><![CDATA[1]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[安庆]]></city>
   <station><![CDATA[安庆市高井头万豪数码广场27-29号方正科技全程服务]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[117.044593]]></xpos>
   <ypos><![CDATA[30.502683]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[2]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[蚌埠]]></city>
   <station><![CDATA[蚌埠市兴业街502号(紫荆名流西面502号商铺)]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[117.389216]]></xpos>
   <ypos><![CDATA[32.931435]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[3]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[亳州]]></city>
   <station><![CDATA[安徽省亳州市希夷大道市政府向北200米路东]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[115.780792]]></xpos>
   <ypos><![CDATA[33.845629]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[4]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[巢湖]]></city>
   <station><![CDATA[安徽巢湖市草城街天和小区14号门面]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[117.872701]]></xpos>
   <ypos><![CDATA[31.625723]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[5]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[池州]]></city>
   <station><![CDATA[安徽省池州市建设中路202号]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[117.485561]]></xpos>
   <ypos><![CDATA[30.660881]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[6]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[滁州]]></city>
   <station><![CDATA[安徽滁州市琅琊东路180号]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[118.317953]]></xpos>
   <ypos><![CDATA[32.302854]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[7]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[安徽]]></province>
   <city><![CDATA[阜阳]]></city>
   <station><![CDATA[阜阳市清河东路361号金协公司3楼西边]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[115.817324]]></xpos>
   <ypos><![CDATA[32.889174]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[331]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[重庆]]></province>
   <city><![CDATA[永川]]></city>
   <station><![CDATA[永川市海通大道103号]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[105.920895]]></xpos>
   <ypos><![CDATA[29.356846]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
<site>
   <id><![CDATA[332]]></id>
   <no><![CDATA[]]></no>
   <province><![CDATA[重庆]]></province>
   <city><![CDATA[重庆]]></city>
   <station><![CDATA[重庆市渝州路60号附B1-3]]></station>
   <pos><![CDATA[]]></pos>
   <xpos><![CDATA[106.485398]]></xpos>
   <ypos><![CDATA[29.532791]]></ypos>
   <ypos><![CDATA[2010-4-9 14:11:19]]></ypos>
</site>
</channel>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值