-1

I have following Three different Nodes Hierarchy (data):

1)
Root
   FirstChild
       leaf
2)     
Root
    SecondChild
       leaf
3)
Root
     ThirdChild
         LeafRoot
             leaf

Result i want as below in One tree :

 Root
    FirstChild
       leaf
    SecondChild
       leaf
    ThirdChild
       LeafRoot
           leaf    

Following Code i am using to get child nodes from three different document.

        NodeList nodeList1 = document1
        .getElementsByTagName("root");

        NodeList nodeList2 = document2
        .getElementsByTagName("root");

        NodeList nodeList3 = document3
        .getElementsByTagName("root");
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61

1 Answers1

1

You can try something like that:

Node rootNode = nodeList1.item(0).appendChild(document1.importNode(nodeList2.item(0).getFirstChild(), true));
rootNode = rootNode.appendChild(document1.importNode(nodeList3.item(0).getFirstChild(), true));
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
  • 1
    This won't work on its own. You first need to use rootNode's Doucment's [importNode](http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html#importNode(org.w3c.dom.Node,%20boolean)) to "adopt" the nodes as its own. Nodes aren't allowed to be shared between trees. – David Ehrmann Feb 22 '14 at 08:50