Java tutorial
//package com.java2s; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static final Object jaxpLock = new Object(); public static Document createDocument(Document document) { return createDocument(document.getDocumentElement()); } public static Document createDocument(Element element) { Document newDocument = getJaxpDocBuilder().newDocument(); Node node = newDocument.importNode(element, true); newDocument.appendChild(node); return newDocument; } public static DocumentBuilder getJaxpDocBuilder() { try { synchronized (jaxpLock) { System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); System.setProperty("javax.xml.parsers.SaxParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance(); docBuildFactory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE); docBuildFactory.setNamespaceAware(true); docBuildFactory.setValidating(false); return docBuildFactory.newDocumentBuilder(); } } catch (ParserConfigurationException pce) { throw new RuntimeException(pce.getMessage()); } } }