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

atxman
atxman used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
But a duplicated node makes no real sense in XML. Why would you do that?
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial