Copying a Subtree of Nodes from One DOM Document to Another - Java XML

Java examples for XML:DOM

Description

Copying a Subtree of Nodes from One DOM Document to Another

Demo Code

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {

  public void main(String[] argv) {
    Document doc1 = null;//from   ww w.  j a  va 2  s . c om
    NodeList list = doc1.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    // Create another document
    Document doc2 = null;

    // Make a copy of the element subtree suitable for inserting into doc2
    Node dup = doc2.importNode(element, true);

    // Insert the copy into doc2
    doc2.getDocumentElement().appendChild(dup);
  }

}

Related Tutorials