Here you can find the source of createDocumentFromElement(final Element element)
public static Document createDocumentFromElement(final Element element) throws ParserConfigurationException, SAXException, IOException
//package com.java2s; // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell import java.io.IOException; 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; import org.xml.sax.SAXException; public class Main { public static Document createDocumentFromElement(final Element element) throws ParserConfigurationException, SAXException, IOException { Document ret = null;/*from www . ja v a2 s . co m*/ if (null == element) { throw new NullPointerException("input DOM element is null"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); ret = factory.newDocumentBuilder().newDocument(); Node domNode = ret.importNode(element, true); ret.appendChild(domNode); return ret; } public static Document createDocumentFromElement(final Node node) throws ParserConfigurationException, SAXException, IOException { Document ret = null; if (null == node) { throw new NullPointerException("input DOM node is null"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); ret = factory.newDocumentBuilder().newDocument(); Node domNode = ret.importNode(node, true); ret.appendChild(domNode); return ret; } }