Java XML Parse Stream parse(InputStream is)

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

Description

This will parse an XML stream and create a DOM document.

License

Apache License

Parameter

Parameter Description
is The stream to get the XML from.

Exception

Parameter Description
IOException It there is an error creating the dom.

Return

The DOM document.

Declaration

public static Document parse(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w .j av  a2 s.c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.InputStream;
import java.io.IOException;
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 {
    /**
     * This will parse an XML stream and create a DOM document.
     *
     * @param is The stream to get the XML from.
     * @return The DOM document.
     * @throws IOException It there is an error creating the dom.
     */
    public static Document parse(InputStream is) throws IOException {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
            builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            builderFactory.setXIncludeAware(false);
            builderFactory.setExpandEntityReferences(false);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            return builder.parse(is);
        } catch (FactoryConfigurationError e) {
            throw new IOException(e.getMessage(), e);
        } catch (ParserConfigurationException e) {
            throw new IOException(e.getMessage(), e);
        } catch (SAXException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
}

Related

  1. parse(InputStream in)
  2. parse(InputStream inputStream)
  3. parse(InputStream inputStream)
  4. parse(InputStream is)
  5. parse(InputStream is)
  6. parse(InputStream is)
  7. parse(InputStream is)
  8. parse(InputStream is)
  9. parse(InputStream s, boolean validateIfSchema)