Flex:使用ActionScript修改hasSimpleContent()==true的节点的XML值

Flex:使用ActionScript修改hasSimpleContent()==true的节点的XML值,xml,apache-flex,actionscript-3,Xml,Apache Flex,Actionscript 3,(来源:) 我所要做的就是修改与树中当前选定节点对应的XML的文本值。除了将“abc”改为“xyz”,一切都是小菜一碟 [Bindable] public var xml:XML= <rootnode> Content A <parentnode Name="value" Attr2="4"> parent content1 <childnode Name="child" Attr2="fun">


(来源:)

我所要做的就是修改与树中当前选定节点对应的XML的文本值。除了将“abc”改为“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? 
    }
  }
[Bindable]
公共变量xml:xml=
内容A
父内容1
子内容
父内容2
abc
;
私有函数XMLTreeLabel(节点:对象):字符串{
if(XMLTree.dataDescriptor.isBranch(节点)){
返回(node.name());
}否则{
返回(node.valueOf());
}
}
私有函数UpdateXMLContent():void
{
var节点:XML=XMLTree.selectedItem为XML;
if((Node.hasSimpleContent())&&
(Node.nodeKind()=“文本”)&&
(Node.valueOf()=“abc”)){
Node='xyz'作为XML;//我得到这行代码,
//但这是不可能修改的
//节点的值
//这些方法都不起作用:
//selectedItem=XML('xyz');
//Node=XML('xyz');
//Node.text()[0]='xyz';
//Node.firstChild.nodeValue='xyz';
//Node.setChildren('xyz');
//节点[0]='xyz';
//更改xml的内容是否不合理/不可能
//对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()"/>


另外,我知道我可以说xml.rootnode.blahblahblah=无论什么,但我需要修改树中表示的条目,因此无法硬编码到节点的路径。

编辑:好的,这已经完全起作用了,祝你好运:

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;
private函数UpdateXMLContent():void
{
var NodeToModify:XML=XMLTree.selectedItem为XML;
if((NodeToModify.hasSimpleContent())&&
(NodeToModify.nodeKind()=“文本”)&&
(NodeToModify.valueOf()=“abc”){
var NodeParent:XML=NodeToModify.parent();
XMLTree.selectedItem=NodeParent.insertchildfter(NodeToModify,“xyz”);
var cn:XMLList=XMLList(NodeToModify.parent()).children();
对于(变量i:Number=0;i
以下是删除元素的解决方案: