Java XML Parse Stream parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)

Here you can find the source of parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)

Description

Parse the XML stream and return the associated W3C Document object.

License

Open Source License

Parameter

Parameter Description
stream The stream to be parsed.
validate True if the document is to be validated, otherwise false.
expandEntityRefs Expand entity References as per DocumentBuilderFactory#setExpandEntityReferences(boolean) .

Return

The W3C Document object associated with the input stream.

Declaration

public static Document parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)
        throws SAXException, IOException 

Method Source Code


//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.ByteArrayInputStream;

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

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**/* ww  w  . j  av a2s  . co m*/
     * Parse the XML stream and return the associated W3C Document object.
     * <p/>
     * Performs a namespace unaware parse.
     *
     * @param stream
     *            The stream to be parsed.
     * @param validate
     *            True if the document is to be validated, otherwise false.
     * @param expandEntityRefs
     *            Expand entity References as per
     *            {@link DocumentBuilderFactory#setExpandEntityReferences(boolean)}.
     * @return The W3C Document object associated with the input stream.
     */
    public static Document parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)
            throws SAXException, IOException {
        return parseStream(stream, validate, expandEntityRefs, false);
    }

    /**
     * Parse the XML stream and return the associated W3C Document object.
     *
     * @param stream
     *            The stream to be parsed.
     * @param validate
     *            True if the document is to be validated, otherwise false.
     * @param expandEntityRefs
     *            Expand entity References as per
     *            {@link DocumentBuilderFactory#setExpandEntityReferences(boolean)}.
     * @param namespaceAware
     *            True if the document parse is to be namespace aware,
     *            otherwise false.
     * @return The W3C Document object associated with the input stream.
     */
    public static Document parseStream(InputStream stream, boolean validate, boolean expandEntityRefs,
            boolean namespaceAware) throws SAXException, IOException {
        if (stream == null) {
            throw new IllegalArgumentException("null 'stream' arg in method call.");
        }
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = null;

            factory.setValidating(validate);
            factory.setExpandEntityReferences(expandEntityRefs);
            factory.setNamespaceAware(namespaceAware);
            docBuilder = factory.newDocumentBuilder();

            return docBuilder.parse(stream);
        } catch (ParserConfigurationException e) {
            IllegalStateException state = new IllegalStateException(
                    "Unable to parse XML stream - XML Parser not configured correctly.");
            state.initCause(e);
            throw state;
        } catch (FactoryConfigurationError e) {
            IllegalStateException state = new IllegalStateException(
                    "Unable to parse XML stream - DocumentBuilderFactory not configured correctly.");
            state.initCause(e);
            throw state;
        }
    }

    /**
     * Parse the supplied XML String and return the associated W3C Document object.
     *
     * @param xml XML String.
     * @return The W3C Document object associated with the input stream.
     */
    public static Document parse(String xml) throws SAXException, IOException {
        return parseStream(new ByteArrayInputStream(xml.getBytes()), false, false);
    }
}

Related

  1. parseMulXML( InputStream is, String[] tagNames)
  2. parseNamespace(XMLStreamReader reader)
  3. parseNoContent(final XMLStreamReader reader)
  4. parseNumber(XMLEventReader stream)
  5. parser(InputStream inputStream)
  6. parseStreamToXML(InputStream in)
  7. parseText(XMLStreamReader parser)
  8. parseValue(XMLStreamReader xmlRdr, String elementName)
  9. parseXml(InputStream in)