Java XML Parse Stream parse(final InputStream inputStream)

Here you can find the source of parse(final InputStream inputStream)

Description

Parses a new XML Document from a given InputStream

License

Apache License

Parameter

Parameter Description
inputStream the InputStream to parse from

Exception

Parameter Description
SAXException if the XML source is not valid
IOException if the source could not be read
ParserConfigurationException if the parser could not be created

Return

the parsed

Declaration

public static Document parse(final InputStream inputStream)
        throws SAXException, IOException, ParserConfigurationException 

Method Source Code


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

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

import java.io.Reader;
import java.io.StringReader;

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

import org.w3c.dom.Document;

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

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

    /**/*from   w w  w . j  a v a2  s.c  o m*/
     * Parses a new XML {@link Document} from a given {@link InputSource}
     * 
     * @param inputSource
     *           the {@link InputSource} to parse from
     * @return the parsed {@link Document}
     * @throws SAXException
     *            if the XML source is not valid
     * @throws IOException
     *            if the source could not be read
     * @throws ParserConfigurationException
     *            if the parser could not be created
     */
    public static Document parse(final InputSource inputSource)
            throws SAXException, IOException, ParserConfigurationException {
        return newDocumentBuilder().parse(inputSource);
    }

    /**
     * Parses a new XML {@link Document} from a given {@link InputStream}
     * 
     * @param inputStream
     *           the {@link InputStream} to parse from
     * @return the parsed {@link Document}
     * @throws SAXException
     *            if the XML source is not valid
     * @throws IOException
     *            if the source could not be read
     * @throws ParserConfigurationException
     *            if the parser could not be created
     */
    public static Document parse(final InputStream inputStream)
            throws SAXException, IOException, ParserConfigurationException {
        return parse(new InputSource(inputStream));
    }

    /**
     * Parses a new XML {@link Document} from a given {@link Reader}
     * 
     * @param reader
     *           the {@link Reader} to parse from
     * @return the parsed {@link Document}
     * @throws SAXException
     *            if the XML source is not valid
     * @throws IOException
     *            if the source could not be read
     * @throws ParserConfigurationException
     *            if the parser could not be created
     */
    public static Document parse(final Reader reader)
            throws SAXException, IOException, ParserConfigurationException {
        return parse(new InputSource(reader));
    }

    /**
     * Parses a new XML {@link Document} from a given XML {@link String}.
     * 
     * @param xmlContent
     *           the XML {@link String} to parse from
     * @return the parsed {@link Document}
     * @throws SAXException
     *            if the XML source is not valid
     * @throws IOException
     *            if the source could not be read
     * @throws ParserConfigurationException
     *            if the parser could not be created
     */
    public static Document parse(final String xmlContent)
            throws SAXException, IOException, ParserConfigurationException {
        return parse(new StringReader(xmlContent));
    }

    /**
     * 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();
    }
}

Related

  1. getNextStartElement(XMLStreamReader parser)
  2. getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory, XMLEventReader parser, XMLEvent paramEvent)
  3. parse(DefaultHandler handler, InputStream in)
  4. parse(final InputStream in)
  5. parse(final InputStream is)
  6. parse(InputStream anInputStream)
  7. parse(InputStream byteStream)
  8. parse(InputStream in)