Using DOM in java to copy a node multiple times and append it to the original element

So I have an xml document that looks like this:
<onixmessage>
    <header>
        <name>thisname</name>
    </header>
    <product>
        <a001>RSN</a001>
        <a002>CODE</a002>
    </product>
</onixmessage>

I want to be able to read this file and load it to a dom document and based on a variable and the product node n times so if I wanted it to repeat 2 times it would look like this:
<onixmessage>
    <header>
        <name>thisname</name>
    </header>
    <product>
        <a001>RSN</a001>
        <a002>CODE</a002>
    </product>
<product>
        <a001>RSN</a001>
        <a002>CODE</a002>
    </product>
</onixmessage>

I am attempting to perform this with the below code but when I print out the new node, it looks like the original.
      public Element cloneXMLNode(Document d, String tagname, int numOfClones) throws TransformerFactoryConfigurationError, TransformerException, IOException{
            //get the last child node of the doc
            Node node = d.getLastChild();
            //normally this will be the same as last child
            Node product = d.getElementsByTagName("product").item(0);
            Element xml = d.getDocumentElement();
            int cntr = 0;
            //adding the product node to the last child n times
            while (cntr++ <= numOfClones){
                  node.appendChild(product);
            }
            //uses the Transformer to print out the new node
            printNode(node, "C:/Test/nodeonly.xml");
            //replacing the new node with the original
            d.replaceChild(node, d.getElementsByTagName("onixmessage").item(0));
            return d.getDocumentElement();
      }

Any ideas as to why this is happening?

Thanks
atxmanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
But a duplicated node makes no real sense in XML. Why would you do that?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I too would question why you are doing that, but I am assuming that you have some reason! ;)

The problem is that you are appending "the same node object" multiple times. It doesn't matter how many times you append it, it is still just ONE node. What you need to do is to create new nodes based on that product node and then append those. There are numerous ways to do this but try the following (untested but hopefully works, or at least gives you the idea)...

public Element cloneXMLNode(Document d, String tagname, int numOfClones) throws TransformerFactoryConfigurationError, TransformerException, IOException{
            Node product = d.getElementsByTagName("product").item(0);
            Element xml = d.getDocumentElement();
            //adding the product node to the last child (n-1) times, -1 because the product node is already there once
            for (int cntr = 0; cntr <= (numOfClones - 1); cntr++){
                  xml.appendChild(product.cloneNode(true));
            }
            //uses the Transformer to print out the new node
            printNode(xml, "C:/Test/nodeonly.xml");
            return xml;
      }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB Dev Tools

From novice to tech pro — start learning today.