Here you can find the source of copy(Document from, Document to)
public static void copy(Document from, Document to)
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void copy(Document from, Document to) { Element fromRootElem = from.getDocumentElement(); Element toRootElem = to.getDocumentElement(); Node childNode, importedNode; NodeList toChildNodes = fromRootElem.getChildNodes(); for (int i = 0; i < toChildNodes.getLength(); i++) { childNode = toChildNodes.item(i); importedNode = to.importNode(childNode, true); toRootElem.appendChild(importedNode); }//from w ww. ja va2 s . c om } }