Java XML Document Create newDocument()

Here you can find the source of newDocument()

Description

Creates and returns a new Document .

License

Apache License

Exception

Parameter Description
ParserConfigurationException if the creation fails

Return

a new

Declaration

public static Document newDocument() throws ParserConfigurationException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

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

public class Main {
    private static DocumentBuilderFactory _dbf = DocumentBuilderFactory.newInstance();

    /**/*from w  w  w . ja  va  2  s. c  o  m*/
     * Creates and returns a new {@link Document}.
     * 
     * @return a new {@link Document}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static Document newDocument() throws ParserConfigurationException {
        return newDocumentBuilder().newDocument();
    }

    /**
     * Creates and returns a new {@link Document} with a root {@link Element}
     * according to the given parameters.
     * 
     * @param nsUri
     *           the namespace URI for the root {@link Element}
     * @param prefix
     *           the prefix for the root {@link Element}
     * @param localName
     *           the local name for the root {@link Element}
     * @return a new according {@link Document}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static Document newDocument(final String nsUri, final String prefix, final String localName)
            throws ParserConfigurationException {
        final String qualifiedName = (prefix == null ? "" : (prefix + ":")) + localName;

        return getDOMImplementation().createDocument(nsUri, qualifiedName, null);
    }

    /**
     * Creates and returns a new {@link Document} with a root {@link Element}
     * according to the given parameters.
     * 
     * @param nsUri
     *           the namespace URI for the root {@link Element}
     * @param localName
     *           the local name for the root {@link Element}
     * @return a new according {@link Document}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static Document newDocument(final String nsUri, final String localName)
            throws ParserConfigurationException {
        return newDocument(nsUri, null, localName);
    }

    /**
     * Creates and returns a new {@link Document} with a root {@link Element}
     * according to the given parameters.
     * 
     * @param localName
     *           the local name URI for the root {@link Element}
     * @return a new according {@link Document}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static Document newDocument(final String localName) throws ParserConfigurationException {
        return newDocument(null, localName);
    }

    /**
     * Creates and returns a new {@link DocumentBuilder}.
     * 
     * @return a new {@link DocumentBuilder}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        return _dbf.newDocumentBuilder();
    }

    /**
     * Creates and returns a new {@link DOMImplementation}.
     * 
     * @return a new {@link DOMImplementation}
     * @throws ParserConfigurationException
     *            if the creation fails
     */
    public static DOMImplementation getDOMImplementation() throws ParserConfigurationException {
        return newDocumentBuilder().getDOMImplementation();
    }
}

Related

  1. newDocument()
  2. newDocument()
  3. newDocument()
  4. newDocument()
  5. newDocument()
  6. newDocument()
  7. newDocument()
  8. newDocument()
  9. newDocument()