XMLで特定ノード以外全削除・サンプルソース
現在開発中のプロジェクトで、Flex3のTreeコントロールにおいて特定ノード
忘れやすい自分のためにFlex備忘録として残します。
・XMLデータから特定のノード(<node>)以外を全部削除する。 ※mmXMLにはXMLデータが設定済みである import mx.collections.XMLListCollection; public var mmXML:XML; private var mmXMLtree:XML; [Bindable] private var mmXlistTree:XMLListCollection; mmXMLtree = mmXML.copy(); // Tree表示用に複製 trace("mmXMLtree.descendants('*')="+mmXMLtree.descendants("*").length()); // <node>以外を全削除 delete_notnode(mmXMLtree.child('*')); trace("mmXMLtree.descendants('*')="+mmXMLtree.descendants("*").length()); trace("mmXML.descendants('*')="+mmXML.descendants("*").length()); mmXlistTree = new XMLListCollection(mmXMLtree.node); // <node>以外を全削除 private function delete_notnode(nlist:XMLList):void { for (var i:int=nlist.length()-1;i>=0;i--) { var na:String = nlist[i].name(); if (na != 'node') { //trace("item="+nlist[i].toXMLString()); delete nlist[i]; } else { var chlist:XMLList = nlist[i].child('*'); //trace ("child:"+chlist.length()) if (chlist.length() > 0) delete_notnode(chlist); } } } <!-- ウインドウ定義 --> <mx:VBox id="TreeBox" width="200" height="100%"> <mx:Label id="tree" text="Tree" /> <mx:Tree id="selkeys" width="100%" height="100%" horizontalScrollPolicy="on" labelField="@TEXT" dataProvider="{mmXlistTree}"> </mx:Tree> </mx:VBox> トレース結果 mmXMLtree.descendants('*')=598 mmXMLtree.descendants('*')=484 mmXML.descendants('*')=598