Java XML Document Create getDocument(URL location, boolean validating, boolean namespaceAware)

Here you can find the source of getDocument(URL location, boolean validating, boolean namespaceAware)

Description

Create a Document representing the XML file at the specified location.

License

BSD License

Parameter

Parameter Description
location the location of an XML document
validating whether the XML parser used in the construction of the document should validate the XML
namespaceAware whether the XML parser used in the construction of the document should be aware of namespaces

Exception

Parameter Description
IOException when there was a problem retrieving or parsing the document.

Return

a Document representing the XML file

Declaration

public static Document getDocument(URL location, boolean validating, boolean namespaceAware)
        throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww  w  .  j  a v  a2  s  .c om*/
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
 *
 * Licensed under the Aduna BSD-style license.
 */

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.validation.Schema;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Main {
    /**
     * Create a Document representing the XML file at the specified location.
     * 
     * @param location
     *            the location of an XML document
     * @return a Document representing the XML file
     * @throws IOException
     *             when there was a problem retrieving or parsing the document.
     */
    public static Document getDocument(URL location) throws IOException {
        return getDocument(location, false, false, null);
    }

    /**
     * Create a Document representing the XML file at the specified location.
     * 
     * @param location
     *            the location of an XML document
     * @param validating
     *            whether the XML parser used in the construction of the
     *            document should validate the XML
     * @param namespaceAware
     *            whether the XML parser used in the construction of the
     *            document should be aware of namespaces
     * @return a Document representing the XML file
     * @throws IOException
     *             when there was a problem retrieving or parsing the document.
     */
    public static Document getDocument(URL location, boolean validating, boolean namespaceAware)
            throws IOException {
        return getDocument(location, validating, namespaceAware, null);
    }

    /**
     * Create a Document representing the XML file at the specified location.
     * 
     * @param location
     *            the location of an XML document
     * @param schema
     *            a Schama instance to validate against
     * @return a Document representing the XML file
     * @throws IOException
     *             when there was a problem retrieving or parsing the document.
     */
    public static Document getDocument(URL location, Schema schema) throws IOException {
        return getDocument(location, false, true, schema);
    }

    private static Document getDocument(URL location, boolean validating, boolean namespaceAware, Schema schema)
            throws IOException {
        Document result = null;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);
        factory.setNamespaceAware(namespaceAware);
        factory.setSchema(schema);

        InputStream in = null;
        try {
            in = new BufferedInputStream(location.openConnection().getInputStream());
            DocumentBuilder builder = factory.newDocumentBuilder();
            result = builder.parse(in);
        } catch (SAXParseException e) {
            String message = "Parsing error" + ", line " + e.getLineNumber() + ", uri " + e.getSystemId() + ", "
                    + e.getMessage();
            throw toIOE(message, e);
        } catch (SAXException e) {
            throw toIOE(e);
        } catch (ParserConfigurationException e) {
            throw toIOE(e);
        } finally {
            if (in != null) {
                in.close();
            }
        }

        return result;
    }

    private static IOException toIOE(Exception e) {
        return toIOE(e.getMessage(), e);
    }

    private static IOException toIOE(String message, Exception e) {
        IOException result = new IOException(message);
        result.initCause(e);
        return result;
    }
}

Related

  1. getDocument(String payload)
  2. getDocument(String xml)
  3. getDocument(String xml)
  4. getDocument(String xmlDocument)
  5. getDocument(String xslName)
  6. getNewDocument()
  7. getNewDocument()
  8. getNewXmlDocument()
  9. newDocument()