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; public class Main { /** * Creates and returns a new empty DOM document. * * @return a newly created DOM document * @throws ParserConfigurationException * @throws ParserConfigurationException */ public static Document newDocument() throws ParserConfigurationException { DocumentBuilder builder = getDocumentBuilder(); return builder.newDocument(); } /** * Creates and returns an new document builder factory. This method tries to * configure the namespace support for the builder. If the underlying parser * does not support namespaces then this method returns a simple * DocumentBuilder object. * * @return a new document builder * @throws ParserConfigurationException */ private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! try { factory.setFeature("http://xml.org/sax/features/namespaces", true); } catch (Throwable t) { // Just skip it... } DocumentBuilder builder = factory.newDocumentBuilder(); return builder; } }