flex tree 节点的添加、删除。

myXml.xml:
<?xml version="1.0" encoding="utf-8"?>
<novel label="单元">
    
    <part label="第一单元">
        <part label="第一单元例题一">
            <part label="练习题一" ></part>
            <part label="练习题二" ></part>
        </part>
        <part label="第一单元例题二">
            <part label="练习题一" ></part>
            <part label="练习题二" ></part>
        </part>
    </part>
    
    <part label="第二单元">
        <part label="第二单元例题一">
            <part label="练习题一" ></part>
            <part label="练习题二" ></part>
        </part>
    </part>
	
    <part label="第三单元">
		
    </part>
</novel>


[Bindable]
public var selectedNode:XML;
public var resultXML:XML;
//初始化.
public function preInit():void{
	var targetURL : String = "myXml.xml";
	var request : URLRequest = new URLRequest(targetURL);
	var variables : URLLoader = new URLLoader();
	variables.addEventListener(Event.COMPLETE, completeHandler);
	try{
		variables.load(request);
	}catch (error:Error){
		trace("无法加载" + error);
	}
}
private function completeHandler(event:Event):void{
	trace(event.target.data is String);
	selectedNode = new XML(event.target.data);
	myTree.dataProvider = selectedNode;
}
			
public function treeChanged(event:Event):void{
	selectedNode = Tree(event.target).selectedItem as XML;
	player.load();
}
//添加节点.
protected function add_clickHandler(event:MouseEvent):void{
	var xml : XML = selectedNode..*.(@label == txtname.text)[0];
	if (xml){
		var xml1 : XML = new XML("<part label=\"" + txtname1.text + "\"></part>");
		xml.appendChild(xml1);
	}else{
		var array : Array = txtname.text.split("/");
		var asd : XML = selectedNode;
		for (var v:int=0; v < array.length; v++){
			var rr : XML = selectedNode..*.(@label == array[v])[0];
			if (rr){
				asd = rr;
				continue;
			}else{
				rr = new XML("<part label=\"" + array[v] + "\"></part>");
				asd.appendChild(rr);
				asd = rr;
			}
		}
		var firstNodeSon1 : XML = new XML("<part label=\"" + txtname1.text + "\"></part>");
		asd.appendChild(firstNodeSon1);
	}
}
//删除节点
protected function del_clickHandler(event:MouseEvent):void{
	var array:Array=txtname.text.split("/");
	var xml:XML=selectedNode..*.(@label == array[array.length - 1].toString())[0];
	if (xml){
		delete xml..*.(@label == txtname1.text)[0];
		if (xml.part.length() == 0){
		    for(var v : int = array.length - 1; v >= 0; v --){
		        var rr : XML = selectedNode..*.(@label == array[v].toString())[0];
			    if(rr.part.length() == 0){
			        delete selectedNode..*.(@label == array[v].toString())[0];
			    }
		    }
		    delete selectedNode..*.(@label == array[array.length - 1].toString())[0];
		}
	}
}


<mx:Tree id="myTree" 
         width="20%" 
         height="100%" 
         labelField="@label" 
         dataProvider="{selectedNode}" 
         showRoot="false" change="treeChanged(event)"
         alpha="0.5"/>

<s:TextInput id="txtname" text=""/>
<s:TextInput id="txtname1" text=""/>
<s:Button label="添加" click="add_clickHandler(event)"/>
<s:Button label="删除" click="del_clickHandler(event)"/>

添加如下图所示:
flex tree 节点的添加、删除。 flex tree 节点的添加、删除。
删除也可以向上图那样输入, 第二个输入框的值的父级节点或父级的父级等等没子节点时, 会把父级以及以上没有子节点的父级一起删除, 否则不会删除有子节点的父级。

你可能感兴趣的:(xml,Flex,ITeye)