Here you can find the source of copyDocument(Document source)
Parameter | Description |
---|---|
source | The Document object to be cloned. |
Parameter | Description |
---|---|
ParserConfigurationException | an exception |
public static Document copyDocument(Document source) throws ParserConfigurationException
//package com.java2s; //License from project: Open Source License import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; public class Main { /**//from w w w. j a va 2s.c o m * Clone a Document object. * * @param source * The Document object to be cloned. * * @return the clone of the Document object * * @throws ParserConfigurationException */ public static Document copyDocument(Document source) throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Node originalRoot = source.getDocumentElement(); Document copiedDocument = db.newDocument(); Node copiedRoot = copiedDocument.importNode(originalRoot, true); copiedDocument.appendChild(copiedRoot); return copiedDocument; } }