2 Replies Latest reply on Aug 14, 2008 4:46 AM by Newsgroup_User

    XML: Apending Children

    TheNut007
      Imagine the following XML:
      <list>
      <item> </item>
      <item> </item>
      </list>

      Now in ActionScript, if I loop through all children and attempt to add text nodes to each individual node like so

      (pseudocode)

      children = xml.children();
      for (i = 0; i < children.length(); ++i)
      {
      children .appendChild("test" + i);
      }

      What happens here is both nodes get updated twice. It looks like the XML code appends the child to all nodes with the same name. So now my XML looks like this:

      <list>
      <item>test1 test2</item>
      <item>test1 test2</item>
      </list>

      What I'm looking to do is this:
      <list>
      <item>test1</item>
      <item>test2</item>
      </list>


      Can this be done or is the XML code in ActionScript borked?
        • 1. Re: XML: Apending Children
          TheNut007 Level 1
          Hmm, I see now that rather than manage multiple xml nodes, flex prefers to group similar nodes into a single array.I'm not particularly fond of this as it goes against how I work with XML in other languages and platforms, but I can live with it for now.

          As a workaround, I'm removing duplicate nodes and instead appending children to the same children over and over again. So the result now looks like this:

          <list>
          <item>
          item1
          item2
          item3
          ...
          </item>
          </list>


          In action script, I have to do the following to access those text nodes:

          list.item.getChildren()[index];

          I guess this isn't to bad. Had it worked my way, the only difference would have been:

          list.item[index]

          So there's an extra step involved. Perhaps I'll just write a wrapper that will do this for me.


          If anyone wants to share their comments on this, please do. Meanwhile I'm going to label this topic answered.
          • 2. Re: XML: Apending Children
            Level 7

            "TheNut007" <webforumsuser@macromedia.com> wrote in message
            news:g80b1v$5jh$1@forums.macromedia.com...
            > Hmm, I see now that rather than manage multiple xml nodes, flex prefers to
            > group similar nodes into a single array.I'm not particularly fond of this
            > as it
            > goes against how I work with XML in other languages and platforms, but I
            > can
            > live with it for now.

            You didn't really say how you were getting your XML that you were looping
            through, but I did suspect you had an XMLList instead of a single XML node.
            But without more information, it wasn't really possible to give a good
            answer to your question. Flex's implementation of e4x is pretty much the
            same as e4x in any language or platform.
            http://dispatchevent.org/roger/as3-e4x-rundown/

            HTH;

            Amy