Java XML Document Parse parseDocument(InputStream documentXml)

Here you can find the source of parseDocument(InputStream documentXml)

Description

Creates a new document object from the given input stream containing the document XML.

License

Open Source License

Parameter

Parameter Description
documentXml the input stream containing the XML

Exception

Parameter Description
Exception if an error occurs

Return

the created document object

Declaration

public static Document parseDocument(InputStream documentXml) throws Exception 

Method Source Code

//package com.java2s;
/**// w  w  w  . j  a v  a2s  . c  o  m
 * The MIT License
 * Copyright (c) 2015 Estonian Information System Authority (RIA), Population Register Centre (VRK)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;

import java.io.InputStream;

public class Main {
    /**
     * Creates a new document object from the given input stream containing the document XML.
     * @param documentXml the input stream containing the XML
     * @return the created document object
     * @throws Exception if an error occurs
     */
    public static Document parseDocument(InputStream documentXml) throws Exception {
        return parseDocument(documentXml, true);
    }

    /**
     * Creates a new document object from the given input stream containing the document XML.
     * @param documentXml the input stream containing the XML
     * @param namespaceAware flag indicating namespace awareness
     * @return the created document object
     * @throws Exception if an error occurs
     */
    public static Document parseDocument(InputStream documentXml, boolean namespaceAware) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        dbf.setNamespaceAware(namespaceAware);
        dbf.setIgnoringComments(true);

        dbf.setValidating(false);
        dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        return dbf.newDocumentBuilder().parse(documentXml);
    }
}

Related

  1. parse(String document)
  2. parseDocument(byte[] bytes)
  3. parseDocument(File f)
  4. parseDocument(File file)
  5. parseDocument(InputSource is)
  6. parseDocument(InputStream in)
  7. parseDocument(InputStream is)
  8. parseDocument(InputStream is)
  9. parseDocument(InputStream is)