change/set E4X text-node text

Ask a Question related to Macromedia Flex General Discussion, Design and Development.

  1. #1

    Default change/set E4X text-node text

    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.

    Gnomann Guest

  2. Similar Questions and Discussions

    1. Text change throughout site
      Hey guys, just a simple problem. Does anyone know if you can edit text on one page and have it change throughout the site. For example, on my site,...
    2. Can you change the color of text with the Touchup Text tool?
      I just want to say that your note helped me. I NEVER knew that I could use a right click to bring up properties when using the text touch-up tool!...
    3. how to change a QueryString to a text Value?
      Hi, I pass, from one page, a single letter querystring which is code for "type" of event. (M = Music, S = Storytelling etc). Here's an...
    4. path to text field to change text
      I received a response (below), but have more questions about how to access a text field in a button in a moviclip that's dynamically created???? ...
    5. change text in existing .gif
      I have a .gif file that I need to make adjustement to the text, specifically, add more text. I do not have the original file or how it was created. I...
  3. #2

    Default Re: change/set E4X text-node text

    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

    ntsiii Guest

  4. #3

    Default Re: change/set E4X text-node text

    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

    ntsiii Guest

  5. #4

    Default Re: change/set E4X text-node text

    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.



    Gnomann Guest

  6. #5

    Default Re: change/set E4X text-node text

    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


    ntsiii Guest

  7. #6

    Default Re: change/set E4X text-node text

    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.

    var xml : XML = <testXML>TEXT NODE TEXT</testXML>;
    trace("xml = " + xml.toXMLString());
    var myTextNode : XML = xml.children()[0];
    trace("My Text Node = " + myTextNode);
    trace("myTextNode is XML?" + (myTextNode is XML));
    xml.children()[0] = "NEW Text Node Text";
    trace("xml = " + xml.toXMLString());
    var newTextNode : XML = xml.children()[0];
    trace("TESTING NODE INSTANCES ARE SAME? " + (myTextNode ===
    newTextNode));
    // now check that myTextNode has parent
    var par : Object = myTextNode.parent();
    if (par is XML){
    trace("HAS XML PaRENT: ");
    trace("MyTextNode Parent ? " + (par as XML).toXMLString());
    } else{
    trace("myTextNode now has no parent");
    }
    trace("newTextNode is now the Child ? " + (newTextNode.parent() ===
    xml));

    Gnomann Guest

  8. #7

    Default Re: change/set E4X text-node text

    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

    ntsiii Guest

  9. #8

    Default Re: change/set E4X text-node text

    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

    Gnomann Guest

  10. #9

    Default Re: change/set E4X text-node text

    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

    Gnomann Guest

  11. #10

    Default Re: change/set E4X text-node text

    Oops, posted that twice.

    Sorry.
    Gnomann Guest

  12. #11

    Default Re: change/set E4X text-node text

    Nobody found a solution to this one?
    Unregistered Guest

  13. #12

    Default Re: change/set E4X text-node text

    Hi
    I had faced the same problem , and came up with a not so elegant but still working solution:
    I dont know if this post is current, but posting in the hope that this will benefit someone :) especially as no real solution could be found on the internet on this topic

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:XML id="mainXML">
    <main>
    <node id="1"><![CDATA[text 1]]></node>
    <node id="2"><![CDATA[text 2]]></node>
    <node id="3"><![CDATA[text 3]]></node>
    </main>
    </mx:XML>
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;

    private function init():void{
    var newVal="NEW TEXT 1"
    var newNode = XML("<![CDATA"+"["+newVal+"]"+"]>")
    mainXML.children().(@id=="1").replace(0, newNode);
    Alert.show("mainXML::"+mainXML)
    }
    ]]>
    </mx:Script>
    </mx:Application>
    mumbaimerijaan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139