Java tutorial
//package com.java2s; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; public class Main { /** * Create document. * * @return the document * @throws Exception * on error */ public static Document createDocument() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document document; try { builder = factory.newDocumentBuilder(); document = builder.newDocument(); } catch (Exception e) { throw e; } return document; } /** * Create document with namespace, qualified name and doc type. * * @param namespaceURI * the namespace URI * @param qualifiedName * the qualified name * @param doctype * the document type * @return the document * @throws Exception * on error */ public static Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document document; try { builder = factory.newDocumentBuilder(); document = builder.getDOMImplementation().createDocument(namespaceURI, qualifiedName, doctype); } catch (Exception e) { throw e; } return document; } }