简体   繁体   English

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

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

alt text
(source: rigel222.com ) (来源: rigel222.com

All I want to do is modify the text value of the XML corresponding to the CURRENTLY SELECTED node in the tree. 我要做的就是修改与树中“当前选定”节点相对应的XML的文本值。 Everything is a piece of cake except actually changing 'abc' to 'xyz'. 除了实际上将“ 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? 
    }
  }

[... 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()"/>

PS 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. PS我知道我可以说xml.rootnode.blahblahblah =什么,但是我需要修改树中表示的条目,因此无法对节点的路径进行硬编码。

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 http://cookbooks.adobe.com/post_Delete_a_node_from_XML___XMLListCollection-5601.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM