Here you can find the source of clone(Document doc)
public static Document clone(Document doc) throws IOException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import org.w3c.dom.Document; import org.w3c.dom.Node; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class Main { /** Clone a document so it can be safely modified on a per-request basis. */ public static Document clone(Document doc) throws IOException { Document d;/*w w w.j a v a 2 s.c o m*/ try { d = newBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new IOException("Cannot clone document"); } Node n = d.importNode(doc.getDocumentElement(), true); d.appendChild(n); return d; } /** Construct a new empty document. */ public static Document newDocument() { try { return newBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new RuntimeException("Cannot create new document", e); } } private static DocumentBuilder newBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); factory.setIgnoringComments(true); factory.setCoalescing(true); return factory.newDocumentBuilder(); } }