0 Replies Latest reply on Apr 21, 2007 1:57 PM by marcboon

    E4X reparenting mystery

    marcboon
      Consider the following code:

      // create a root node with 2 child nodes
      var root:XML = <node id="1" />;
      root.appendChild(<node id="2" />);
      root.appendChild(<node id="3" />);
      trace(root.toXMLString());

      result:
      <node id="1">
      <node id="2"/>
      <node id="3"/>
      </node>

      // now get the child nodes by their id
      var node2:XML = root.node.(@id == 2)[0];
      var node3:XML = root.node.(@id == 3)[0];

      // make node3 a child of node2, this will create a copy of node3 under node2
      node2.appendChild(node3);
      trace(root.toXMLString());

      result:
      <node id="1">
      <node id="2">
      <node id="3"/>
      </node>
      <node id="3"/>
      </node>

      // now try to delete the original node3 directly under the root node
      delete root.node.(@id == 3)[0];
      trace(root.toXMLString());

      result:
      <node id="1">
      <node id="2"/>
      <node id="3"/>
      </node>

      The wrong node gets deleted!
      How do I move (reparent) a node with E4X?