Java XML Document Create createDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID)

Here you can find the source of createDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID)

Description

Creates empty DOM Document using JAXP factoring.

License

Sun Public License Notice

Parameter

Parameter Description
rootQName qualified name of root element. e.g. <code>myroot</code> or <code>ns:myroot</code>
namespaceURI URI of root element namespace or <code>null</code>
doctypePublicID public ID of DOCTYPE or <code>null</code>
doctypeSystemID system ID of DOCTYPE or <code>null</code> if no DOCTYPE required and doctypePublicID is also <code>null</code>

Exception

Parameter Description
DOMException if new DOM with passed parameters can not be created
FactoryConfigurationError Application developers should never need to directly catch errors of this type.

Return

new DOM Document

Declaration

public static Document createDocument(String rootQName, String namespaceURI, String doctypePublicID,
        String doctypeSystemID) throws DOMException 

Method Source Code

//package com.java2s;
/*/*from  www .ja v a2  s.  co m*/
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

public class Main {
    /**
     * Creates empty DOM Document using JAXP factoring. E.g.:
     * <p><pre>
     * Document doc = createDocument("book", null, null, null);
     * </pre><p>
     * creates new DOM of a well-formed document with root element named book.
     *
     * @param rootQName qualified name of root element. e.g. <code>myroot</code> or <code>ns:myroot</code>
     * @param namespaceURI URI of root element namespace or <code>null</code>
     * @param doctypePublicID public ID of DOCTYPE or <code>null</code>
     * @param doctypeSystemID system ID of DOCTYPE or <code>null</code> if no DOCTYPE
     *        required and doctypePublicID is also <code>null</code>
     *
     * @throws DOMException if new DOM with passed parameters can not be created
     * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
     *
     * @return new DOM Document
     */
    public static Document createDocument(String rootQName, String namespaceURI, String doctypePublicID,
            String doctypeSystemID) throws DOMException {
        DOMImplementation impl = getDOMImplementation();

        if ((doctypePublicID != null) && (doctypeSystemID == null)) {
            throw new IllegalArgumentException("System ID cannot be null if public ID specified. "); //NOI18N
        }

        DocumentType dtd = null;

        if (doctypeSystemID != null) {
            dtd = impl.createDocumentType(rootQName, doctypePublicID, doctypeSystemID);
        }

        return impl.createDocument(namespaceURI, rootQName, dtd);
    }

    /**
     * Obtains DOMImpementaton interface providing a number of methods for performing
     * operations that are independent of any particular DOM instance.
     *
     * @throw DOMException <code>NOT_SUPPORTED_ERR</code> if cannot get DOMImplementation
     * @throw FactoryConfigurationError Application developers should never need to directly catch errors of this type.
     *
     * @return DOMImplementation implementation
     */
    private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            return factory.newDocumentBuilder().getDOMImplementation();
        } catch (ParserConfigurationException ex) {
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
                    "Cannot create parser satisfying configuration parameters"); //NOI18N
        } catch (RuntimeException e) {
            // E.g. #36578, IllegalArgumentException. Try to recover gracefully.
            throw (DOMException) new DOMException(DOMException.NOT_SUPPORTED_ERR, e.toString()).initCause(e);
        }
    }
}

Related

  1. createDocument(String root, NodeList content)
  2. createDocument(String rootElement)
  3. createDocument(String rootElementName)
  4. createDocument(String rootName)
  5. createDocument(String rootNodeName)
  6. createDocument(String str)
  7. createDocument(String xml)
  8. createDocumentBuilder()
  9. createDocumentBuilder(boolean namespaces, boolean validating)