Java tutorial
//package com.java2s; //License from project: LGPL import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class Main { public static Document documentFromNode(Element element) { try { DocumentBuilder dBuilder = getDocumentBuilder(); Document newDocument = dBuilder.newDocument(); Node importedNode = newDocument.importNode(element, true); newDocument.appendChild(importedNode); return newDocument; } catch (Exception e) { throw new RuntimeException(e); } } private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); setUpSecurity(dbFactory); return dbFactory.newDocumentBuilder(); } private static void setUpSecurity(DocumentBuilderFactory dbFactory) throws ParserConfigurationException { dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbFactory.setXIncludeAware(false); dbFactory.setExpandEntityReferences(false); } }