Programming : Copying a Node from One DOM Document to Another

This page last changed on Oct 12, 2006 by Kees de Kooter

public Document copyNode(Node originalNode) {

    DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
    DocumentBuilder builder  = factory.newDocumentBuilder();
    Document newDocument = builder.newDocument();

    newDocument.appendChild(newDocument.importNode(originalNode, true);

    return newDocument;
}