Java tutorial
//package com.java2s; //License from project: Open Source License import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.*; public class Main { private static DocumentBuilder m_builder; /** * Creates a new {@link Document}. * * @return Returns said {@link Document}. */ public static Document createDocument() { return m_builder.newDocument(); } /** * Creates a new {@link Document}. * * @param rootElementName The name of the root element. * @return Returns said {@link Document}. */ public static Document createDocument(String rootElementName) { return createDocument(rootElementName, null); } /** * Creates a new {@link Document}. * * @param rootElementName The name of the root element. * @param xmlns The namespace of the document. * @return Returns said {@link Document}. */ public static Document createDocument(String rootElementName, String xmlns) { final Document doc = createDocument(); final Element root = doc.createElement(rootElementName); if (xmlns != null) root.setAttribute("xmlns", xmlns); doc.appendChild(root); return doc; } }