Java tutorial
//package com.java2s; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class Main { public static Document newDocument(boolean namespaceaware) throws Exception { DocumentBuilder builder = getSyncDocumentBuilder(namespaceaware); Document doc = builder.newDocument(); builder.reset(); return doc; } public static DocumentBuilder getSyncDocumentBuilder(boolean namespaceAware) throws ParserConfigurationException { return getSyncDocumentBuilder(namespaceAware, false, false); } public static synchronized DocumentBuilder getSyncDocumentBuilder(boolean namespaceAware, boolean coalescing, boolean ignoringElementContentWhitespace) throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(namespaceAware); domFactory.setCoalescing(coalescing); domFactory.setIgnoringElementContentWhitespace(ignoringElementContentWhitespace); return domFactory.newDocumentBuilder(); } }