首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

遍历xml数据,展示三层树

2012-12-19 
遍历xml数据,显示三层树?xml version1.0 encodingUTF-8?menuListmenu name菜单1 urldownl

遍历xml数据,显示三层树

<?xml version="1.0" encoding="UTF-8"?><menuList><menu name="菜单1" url="download.php" swf="icon1.swf"><cmenu name="闯关1" url="onservice.php">   <award id="10001" lv="1"/>   <award id="10002" lv="2"/></cmenu><cmenu name="闯关2" url="onservice1.php"/><cmenu name="闯关3" url="onservice2.php"/><cmenu name="闯关4" url="onservice.php">   <award id="10003" lv="1"/>   <award id="10004" lv="2"/></cmenu></menu><menu name="菜单2" url="news.php" swf="icon1.swf"><cmenu name="INTEL" url="onservice.php"/><cmenu name="AMD" url="onservice.php"/><cmenu name="XFX" url="onservice.php"/><cmenu name="MSI" url="onservice.php"/></menu></menuList>

package{  import flash.display.*;  import flash.events.*;  import flash.net.*;  import flash.text.*;  public class menu extends Sprite {    private var url:URLLoader;    private var sprite:Sprite;    private var mainsprite:Sprite;    private var test:TextField;    private var ctest:TextField;    private var xml:XML;    private var _xml:XML;    public function menu(){       url = new URLLoader;       url.load(new URLRequest("MenuList.xml"));       url.addEventListener(Event.COMPLETE,handcomplete);    }    private function handcomplete(event:Event){       xml = new XML(event.target.data);       _xml = xml;       for(var i:int=0;i<xml.menu.length();i++){          var mname:String=xml.menu[i].@name;  test = new TextField;  test.border=true;  test.selectable=false;  test.text=mname;  test.x=20;  test.y=i*test.height+20;  test.name=String(i); //记录下标  test.addEventListener(MouseEvent.CLICK,dispChildMenu);  addChild(test);       }    }    private function dispChildMenu(event:MouseEvent):void    {        if(sprite){  for (var k:int=sprite.numChildren - 1; k >=0; k--){     sprite.removeChildAt(k);  }}else{  sprite = new Sptite();}for(var j:int=this.numChildren-1;j>=0;j--){   if(j>event.target.name){     this.removeChildAt(j);   }}addChild(sprite);        var len:Number = new Number(event.target.name);        var _mc:MovieClip;if(xml.menu[len].length()>0){   for(j=0;j<xml.menu[len].cmenu.length();j++){     _mc = new MovieClip();     var cname:String=xml.menu[len].cmenu[j].@name;     ctest = new TextField;     ctest.border=true;     ctest.autoSize="left";     _mc.x = event.target.x;             _mc.y = ctest.height*(j+1);   var awardData : Array = new Array();   //重要     for(var m:int=0;m<xml.menu[len].cmenu[j].award.length();m++){  //重要        var tob:Object = new Object();                          //重要for each(var d in xml.menu[len].cmenu[j].award[m].attributes()){  //重要  tob[String(d.name())] = d;                              //重要}awardData.push(tob);                                      //重要     }     _mc.awarddata = awardData;     _mc.addChild(ctest);     _mc.addEventListener(MouseEvent.CLICK,dispThirdMenu);     sprite.addChild(_mc);   }}for(var i:int=0;i<_xml.menu.length();i++){   if(i>event.target.name){      var mname = _xml.menu[i].@name;      test = new TextField;      test.border=true;      test.autoSize="left";      test.text = mname;      test.x=20;      test.y = i*test.height+76;      test.name=String(i);      test.addEventListener(MouseEvent.CLICK,dispChildMenu);      addChild(test);      }}    }    function dispThirdMenu(event:MouseEvent):void{       for(var k:int=0;k<event.currentTarget.awarddata.length;k++){           trace(event.currentTarget.awarddata[k].id);   trace(event.currentTarget.awarddata[k].lv);              }        }      }}


删除容器子对象有两种写法:

1. for(var k:int=sprite.NumChildren-1;k>=0;k--){
           sprite.removeChildAt(k);
    }

2. var k:uint = sprite.numChildren;
    whild(k>0){
     sprite.removeChildAt(k);
     k--;
    }
由于显示列表会自动移动显示对象填充空缺,所以只移除0索引的位置即可。这样效率最高。


//输出:con's children:8   graphics对象绘制的图形果然不在其中。

摘自:http://blog.sina.com.cn/s/blog_5a6f39cf0100njuv.html

热点排行