4 Replies Latest reply on May 4, 2006 1:53 PM by rico6xrico

    How to copy an XML Tree Node Structure using Flash?

    rico6xrico
      Hello All,

      I'm trying to figure out how to copy a xml tree NODE structure using
      flash. Meaning, i create a new Node (newNode), and then I want the
      newNode to have the same structure - ie. tag name as what my current
      XML tree Nodes have (xmlData). Then, I want to be able to add new
      values to newNode and append it to my xmlData tree.

      I have tried the following but does not work.

      ////////////////////////////// FLASH CODE //////////////////////

      var newNode = new XMLNode();
      newNode = xmlData.firstChild.childNodes[1];

      newNode.childNodes[0].firstChild.nodeValue = new Date(month, date,
      year).toString();
      newNode.childNodes[1].firstChild.nodeValue = myName.text;
      newNode.childNodes[2].firstChild.nodeValue = myURL.text;

      xmlData.appendChild(newNode);

      ////////////////////////////////////////////my XML Data
      structure//////////////////

      <List>
      <day date="March 6, 2006">
      <A>March 6, 2006</A>
      <L>theName</L>
      <U>theURL</U>
      </day>
      </List>

      ////////////////////////////////////////////////////////////////////////////////////////

      Please help, if there is a better way, let me know.
        • 1. Re: How to copy an XML Tree Node Structure using Flash?
          TimSymons Level 1
          Try using the cloneNode method.

          Tim
          • 2. Re: How to copy an XML Tree Node Structure using Flash?
            rico6xrico Level 1
            Usage

            my_xml.cloneNode(deep)

            Parameters

            deep Boolean value specifying whether the children of the specified XML object are recursively cloned.

            Returns

            An XML node.

            ---------------------------

            How would I use cloneNode to clone only one node of the xml tree and not the entire list?

            I suppose I'm looking for syntax. So, if i just wanted to clone the firstChild or lastChild listed on my example.

            I'm trying to clone or create a new node with the following structure.

            <day date="March 6, 2006">
            <A>March 6, 2006</A>
            <L>theName</L>
            <U>theURL</U>
            </day>
            • 3. Re: How to copy an XML Tree Node Structure using Flash?
              TimSymons Level 1
              Let's say you have an XML tree that has 4 child nodes and you want to clone child 2.

              var xmlClone:XMLNode;
              xmlClone = xmlTree.childNodes[1].cloneNode(true);

              This will put a copy of the 2nd node of you XML tree into xmlClone along with all children attached to the 2nd node. It will not copy anything attached to the any of the other nodes.

              Tim
              • 4. Re: How to copy an XML Tree Node Structure using Flash?
                rico6xrico Level 1
                Tim,

                Your systax did work, but now i'm trying to update the data value for the xmlClone Node. Can you show me the syntax to change the information, nodeValue.

                I'm trying the following:

                newNode.childNodes[0].firstChild.nodeValue = new Date(month, date, year).toString();
                newNode.childNodes[1].firstChild.nodeValue = "rico";
                newNode.childNodes[2].firstChild.nodeValue = "www.google.com";


                but instead of have just one Node on my XML file, i have 3 notes with the same information.

                ////XML FILE

                <day date="March 6, 2006"><D>Thu May 4 16:42:17 GMT-0400 2006</D><L>Rico Google</L><U>www.google.com</U></day>

                <day date="March 6, 2006"><D>Thu May 4 16:42:18 GMT-0400 2006</D><L>Rico Google</L><U>www.google.com</U></day>

                <day date="March 6, 2006"><D>Thu May 4 16:43:22 GMT-0400 2006</D><L>Rico Google</L><U>www.google.com</U></day>