2010-01-12 34 views
0

alt text http://www.rigel222.com/Clip0021.jpg 我想要做的就是修改對應於樹中CURRENTLY SELECTED節點的XML的文本值。除了實際上將'abc'改爲'xyz'之外,一切都是小菜一碟。Flex:修改valueOf XML的節點hasSimpleContent()== true使用ActionScript

[Bindable] 
    public var xml:XML= 
    <rootnode> 
    Content A 
    <parentnode Name="value" Attr2="4"> 
     parent content1 
     <childnode Name="child" Attr2="fun"> 
     child content 
     </childnode> 
     parent content 2 
    </parentnode> 
    abc   <!-- Selected Node When Button Is Pressed--> 
    </rootnode> 
    ; 

    private function XMLTreeLabel(node:Object):String{ 
    if (XMLTree.dataDescriptor.isBranch(node)) { 
     return(node.name()); 
    } else { 
     return(node.valueOf()); 
    } 
    } 
    private function UpdateXMLContent():void 
    { 
    var Node:XML=XMLTree.selectedItem as XML; 
    if ((Node.hasSimpleContent())&& 
     (Node.nodeKind()=="text")&& 
     (Node.valueOf()=="abc")) { 
     Node='xyz' as XML; // I Get To This Line Of Code, 
         // But It Is Impossible To MODIFY 
         // The Value Of The Node 
     // None of these ways work either: 
     // XMLTree.selectedItem=XML('xyz'); 
     // Node=XML('xyz'); 
     // Node.text()[0]='xyz'; 
     // Node.firstChild.nodeValue='xyz'; 
     // Node.setChildren('xyz'); 
     // Node[0]='xyz'; 
     // Is changing the content of xml an unreasonable/impossible 
     // expectation for Flex? 
    } 
    } 

[...一些代碼省略...]

<mx:Tree height="100%" width="100%" 
     dataProvider="{xml}" labelFunction="XMLTreeLabel" 
     id="XMLTree"></mx:Tree> 
    <mx:Button x="185" y="335" label="Button" 
     click="UpdateXMLContent()"/> 

P.S.我知道我可以說xml.rootnode.blahblahblah =無論如何,但我需要修改樹中表示的條目,因此不能硬編碼到節點的路徑。

回答

0

編輯:好吧,這完全是現在的工作,好運氣:

private function UpdateXMLContent():void 
{ 
    var NodeToModify:XML=XMLTree.selectedItem as XML; 
    if ((NodeToModify.hasSimpleContent())&& 
     (NodeToModify.nodeKind()=="text")&& 
     (NodeToModify.valueOf()=="abc")) {     
     var NodeParent:XML = NodeToModify.parent(); 
     XMLTree.selectedItem = NodeParent.insertChildAfter(NodeToModify,"xyz"); 

     var cn:XMLList = XMLList(NodeToModify.parent()).children(); 

     for (var i:Number = 0 ; i < cn.length() ; i++) 
     { 
      if (cn[i] == NodeToModify) 
      { 
       delete cn[i]; 
       trace(NodeParent); 
       return; 
      } 
     }  

     //we didn't find the node, shouldn't be reached 
     return; 

下面是刪除元素的溶液:

http://cookbooks.adobe.com/post_Delete_a_node_from_XML___XMLListCollection-5601.html