Java XML Parse Stream parseXml(InputStream inputStream, boolean validating)

Here you can find the source of parseXml(InputStream inputStream, boolean validating)

Description

Parses an XML input stream and returns a DOM document.

License

Open Source License

Parameter

Parameter Description
inputStream a parameter
validating a parameter

Exception

Parameter Description
ParserConfigurationException an exception
IOException an exception
SAXException an exception

Declaration

public static Document parseXml(InputStream inputStream, boolean validating)
        throws DOMException, ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
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.ParserConfigurationException;

import org.w3c.dom.DOMException;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    private static DocumentBuilder domBuilder = null;

    /**/* w  w  w.j a v a2s . c o m*/
     * Parses an XML input stream and returns a DOM document. If validating is true, the contents is validated against the DTD specified in
     * the file.
     * 
     * @param inputStream
     * @param validating
     * @return
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static Document parseXml(InputStream inputStream, boolean validating)
            throws DOMException, ParserConfigurationException, SAXException, IOException {
        if (domBuilder == null) {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);
            // Configure it to ignore comments
            factory.setIgnoringComments(true);
            // check namespace
            factory.setNamespaceAware(true);

            // Create the builder and parse the file
            domBuilder = factory.newDocumentBuilder();
            // if (!domBuilder.getDOMImplementation().hasFeature("traversal", "2.0")) {
            // supportTraversal = false;
            // }
        }
        Document doc = domBuilder.parse(inputStream);
        // do the normalization
        doc.normalize();

        return doc;

    }

    public static Document parseXml(String xmlContent, boolean validating)
            throws DOMException, ParserConfigurationException, SAXException, IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(xmlContent.getBytes());
        return parseXml(bis, validating);
    }
}

Related

  1. parseText(XMLStreamReader parser)
  2. parseValue(XMLStreamReader xmlRdr, String elementName)
  3. parseXml(InputStream in)
  4. parseXml(InputStream in)
  5. parseXML(InputStream in)
  6. parseXML(InputStream is)
  7. parseXML(InputStream is, String[] tagNames)
  8. parseXml(InputStream src)
  9. parseXMLFromStream(InputStream stream)