遍历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); } } }}