Search code examples
xmlapache-flexactionscript-3

Flex: Modifying valueOf XML for node where hasSimpleContent()==true using ActionScript


alt text
(source: rigel222.com)

All I want to do is modify the text value of the XML corresponding to the CURRENTLY SELECTED node in the tree. Everything is a piece of cake except actually changing 'abc' to 'xyz'.

  [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? 
    }
  }

[... some code omitted...]

    <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. I know I can say xml.rootnode.blahblahblah = whatever, but I need to modify the entry represented in the tree, and therefore cannot hard code the path to the node.


Solution

  • Edit: Ok, this is fully working now, good luck:

    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;
    

    Here's the solution for deleting the element:

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