9 Replies Latest reply on Apr 30, 2008 5:44 PM by Gnomann

    change/set E4X text-node text

    Gnomann
      Hello all,

      Trying to change/set the text-value of an AS3 E4X text node (nodeKind == 'text': TRUE).

      Assuming that 'textNode' is a valid E4X text node, some folks on the web have offered this as a solution...

      textNode.parent().children()[textNode.childIndex()] = "newTextValue";

      (Or Broken into multiple statements):

      var p : XML = textNode.parent();
      var childIndex : int = textNode.childIndex();
      p.children()[childIndex] = "newTextValue";

      Unfortunately, this actually removes 'textNode' from the parent, replacing it with a new text-node with the "newTextValue" as its text (inserting the new-text-node where textNode was).

      This is not the behavior I'm looking for. I want to explicitly set the text value of 'textNode'.

      Is there a way to do this? If not, why not? The old XMLNode allowed this via the .nodeValue property.

      Thanks mucho for any help.
        • 1. Re: change/set E4X text-node text
          ntsiii Level 3
          I am not following. If you set the value of a text node, it replaces the previous text node.

          What are you seeing as the difference between setting nodeValue and assigning a string to a text node?

          I haven't analyzed this, so I can say definitively that there is no difference, but what, functionally speaking, are you trying to achieve?

          Tracy
          • 2. Re: change/set E4X text-node text
            ntsiii Level 3
            I am not following. If you set the value of a text node, it replaces the previous text node.

            What are you seeing as the difference between setting nodeValue and assigning a string to a text node?

            I haven't analyzed this, so I can say definitively that there is no difference, but what, functionally speaking, are you trying to achieve?

            Tracy
            • 3. Re: change/set E4X text-node text
              Gnomann Level 1
              Thanks for the reply...

              Note that this is for an EX4 text-node, and not the old XMLNode from AS2. the XML(Node) from AS2 had a 'nodeValue' property. The E4X (new) xml-nodes (instances of AS3 class XML) do not. There does not seem to be a way to set an E4X text-node's text-value. (There's no 'nodeValue', or any method that would seem to allow it (setText/setTextValue/etc.), for example.

              Essentially, what is happening by the offered solution is that the explicit textNode (or I guess I should have called it myTextNode, which I will from here-onwared), is not having its text-value set. By this method, a new text-node (call it 'newTextNode', an instance of XML) is created, and it replaces myTextNode in the myTextNode's parent. Thus, myTextNode becomes an orphan (is no longer part of the parenting XML-tree) and its old text-value remains the same.

              So, if in my program I have a reference to myTextNode (an explicit XML node instance), and I want to change it's text-value, I can't. What I have to do is perform the given method, and then make sure that I reset all references in my program to myTextNode to the newTextNode that was created. An annoyance to say the least.

              So what I'm looking for is a way to achieve the same effect of setting .nodeValue for an AS2 text-type XMLNode instance (thus changing its text value), but in the AS3 E4X XML node instance.

              If there is no way to do this, the E4X text-nodes are simply immutable.

              Am I missing something? If I can't 'reset' an E4X (XML) text-node's text, then I may simply have to go back to the older (AS2) version XMLNode which is still included in the AS3/Flex class-library for backwards compatability.

              Thanks for any further response.

              • 4. Re: change/set E4X text-node text
                ntsiii Level 3
                No, something is wrong in what you are doing. I work with text nodes regularly without any problem. No lost references or orphaned text nodes.

                How are you getting and storing your reference to the text node?

                Perhaps you should post (use the Attach Code Button please) a small test case that shows the behavior you are seeing. I suspect that creating such an example will show up the problem.

                Tracy
                • 5. Re: change/set E4X text-node text
                  Gnomann Level 1
                  Allright,

                  Thanks for the help.

                  Here's (Attached, I hope) is a sample code snippet.

                  The output (trace) looks like this:

                  xml = <testXML>TEXT NODE TEXT</testXML>
                  My Text Node = TEXT NODE TEXT
                  myTextNode is XML?true
                  xml = <testXML>NEW Text Node Text</testXML>
                  TESTING NODE INSTANCES ARE SAME? false
                  myTextNode now has no parent
                  newTextNode is now the Child ? true

                  Note that now the XML has a child 0 that is a new XML instance. It's not the same node.

                  Thus, I'm now pointing (via 'myTextNode') to the original node, now orphaned, having been replacted by a new text-node with the new text.

                  So I want to be able to change the 'contained' text of myTextNode, directly.

                  Thank you for any further help.
                  • 6. Re: change/set E4X text-node text
                    ntsiii Level 3
                    I haven't run that yet, but granting what you say, that is interesting. It occurs to me that I may have never manipulated direct references to text nodes themselves. I pretty much always work with references to the parent nodes.

                    What is your business case for needing the ref to the text node instead of its parent?

                    Also, you might want to look into the e4x (ecma) specifications. they might explain what is happening.

                    Also, e4x is bigger than AS, expand your question to google.

                    Tracy
                    • 7. Re: change/set E4X text-node text
                      Gnomann Level 1
                      Thanks for the reply.

                      The 'business' case, amongst possible others, is any direct editing of an XML tree (such as one derived from a file). For instance, in a treeview (AS Tree control) one can provide visual items that represent the text-nodes themselves (which is sometimes necessary/desirable, such as if the parent has multiple text-node children). So if a user selects the text-node itself (represented by an item in the tree-control) for changing/editing, the logic of the program, given that one can't simply reset the text-node text value, now has to grab the parent, reset the text via the childIndex, then reset all references to the newly replacing node. A simple case is if the app holds a ref to the composite/overall view's '.userSelectedItem', for instance. If that variable had been set when the text-node was selected for editing, it then has to be reset when the new text-node is created.

                      The problem gets more complicated for more advanced app-systems.

                      I've already implemented a work-around (basically just brute-force resetting all references/views), which is going to be quicker than reverting to the old AS2 XMLNode trees, but I was rather stunned when I became pretty sure I was going to have to do this...

                      I'll check out the E4X refs but it really won't do me any good at this point, as I'm Flex/AIR-bound and so have to deal with Adobe's implementation. I'm beginning to wonder if they've mis-implemented the spec. I'll find out when I check out the E4X docs.

                      If you ever find a way to set the text-node text directly, please do tell. Note also that I'd recommend to Adobe that they even "extend" the E4X model a tad to handle this even if the spec doesn't call for it. Would just a little bit of systemic-sugar.

                      Thanks for your help.

                      -- Cheers
                      • 8. Re: change/set E4X text-node text
                        Gnomann Level 1
                        Thanks for the reply.

                        The 'business' case, amongst possible others, is any direct editing of an XML tree (such as one derived from a file). For instance, in a treeview (AS Tree control) one can provide visual items that represent the text-nodes themselves (which is sometimes necessary/desirable, such as if the parent has multiple text-node children). So if a user selects the text-node itself (represented by an item in the tree-control) for changing/editing, the logic of the program, given that one can't simply reset the text-node text value, now has to grab the parent, reset the text via the childIndex, then reset all references to the newly replacing node. A simple case is if the app holds a ref to the composite/overall view's '.userSelectedItem', for instance. If that variable had been set when the text-node was selected for editing, it then has to be reset when the new text-node is created.

                        The problem gets more complicated for more advanced app-systems.

                        I've already implemented a work-around (basically just brute-force resetting all references/views), which is going to be quicker than reverting to the old AS2 XMLNode trees, but I was rather stunned when I became pretty sure I was going to have to do this...

                        I'll check out the E4X refs but it really won't do me any good at this point, as I'm Flex/AIR-bound and so have to deal with Adobe's implementation. I'm beginning to wonder if they've mis-implemented the spec. I'll find out when I check out the E4X docs.

                        If you ever find a way to set the text-node text directly, please do tell. Note also that I'd recommend to Adobe that they even "extend" the E4X model a tad to handle this even if the spec doesn't call for it. Would just a little bit of systemic-sugar.

                        Thanks for your help.

                        -- Cheers
                        • 9. Re: change/set E4X text-node text
                          Gnomann Level 1
                          Oops, posted that twice.

                          Sorry.