Java XML Parse Stream parse(InputStream stream)

Here you can find the source of parse(InputStream stream)

Description

parse

License

Open Source License

Parameter

Parameter Description
stream a parameter

Exception

Parameter Description
IOException an exception
SAXException an exception
ParserConfigurationException an exception

Declaration

public synchronized static Document parse(InputStream stream)
        throws IOException, SAXException, ParserConfigurationException 

Method Source Code

//package com.java2s;
/* $This file is distributed under the terms of the license in /doc/license.txt$ */

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

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 DocumentBuilder parser;

    /**/*w  w  w  .  j a v  a 2s  .  c o m*/
     * @param xmlString
     * @return
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public synchronized static Document parse(String xmlString)
            throws IOException, SAXException, ParserConfigurationException {
        StringReader reader = new StringReader(xmlString);
        InputSource inputSource = new InputSource(reader);
        return getDocumentBuilder().parse(inputSource);
    }

    /**
     * @param stream
     * @return
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public synchronized static Document parse(InputStream stream)
            throws IOException, SAXException, ParserConfigurationException {
        return getDocumentBuilder().parse(stream);
    }

    /**
     * @return
     * @throws ParserConfigurationException
     */
    public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        if (parser == null) {
            // JPT: Remove xerces use
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            documentBuilderFactory.setValidating(false);
            parser = documentBuilderFactory.newDocumentBuilder();
        }

        return parser;
    }
}

Related

  1. parse(InputStream is)
  2. parse(InputStream is)
  3. parse(InputStream is)
  4. parse(InputStream is)
  5. parse(InputStream s, boolean validateIfSchema)
  6. parse(XMLStreamReader reader)
  7. parse(XMLStreamReader reader, String... elementNames)
  8. parseDoc(final InputStream is)
  9. parseInputStream(InputStream in, boolean namespaces, boolean validating)