Java XML Document from Stream readXML(InputStream inStream)

Here you can find the source of readXML(InputStream inStream)

Description

Reads the argument XML input stream.

License

Open Source License

Parameter

Parameter Description
inStream An input stream carrying the only subject XML

Return

The root node of the read DOM document.

Declaration

public static Node readXML(InputStream inStream) 

Method Source Code


//package com.java2s;
/*/* ww  w .  j a  v a 2  s  .  c o  m*/
 *  $Id$
 *
 *  This code is derived from public domain sources. Commercial use is allowed.
 *  However, all rights remain permanently assigned to the public domain.
 *
 *  XMLUtils.java : A class of static XML utility methods.
 *
 *  Copyright (C) 2009, 2010  Scott Herman
 *
 *  This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this code.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.InputStream;
import java.io.IOException;
import java.io.StringReader;

import java.net.URI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

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

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Reads the argument XML input stream.
     * @param inStream An input stream carrying the only subject XML
     * @return The root node of the read DOM document.
     */
    public static Node readXML(InputStream inStream) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(inStream);
            inStream.close();
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException(null, saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException(null, ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException(null, parsEx);
        } // try - catch
    }

    /**
     * Reads the argument XML input String.
     * @param xmlString The XML input String to be parsed.
     * @return The root node of the read DOM document.
     */
    public static Node readXML(String xmlString) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
        } // try - catch
    }

    public static Node readXML(URI uri) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(uri.toString());
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
        } // try - catch
    }
}

Related

  1. loadXMLFrom(final InputStream is)
  2. loadXmlFromInputSource(InputSource is)
  3. readXML(InputStream input)
  4. readXml(InputStream inputStream)
  5. readXML(InputStream inputStream, EntityResolver resolver, ErrorHandler error)
  6. readXML(InputStream is)