Java XML Parse Stream parseStreamToXML(InputStream in)

Here you can find the source of parseStreamToXML(InputStream in)

Description

parse Stream To XML

License

Open Source License

Declaration

public static Document parseStreamToXML(InputStream in) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .ja v a2s  .  c  o m*/
 * Keystone Development Framework
 * Copyright (C) 2004-2009 Rick Knowles
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 2 for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

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 {
    public static Document parseStreamToXML(InputStream in) {
        try {
            // Use JAXP to create a document builder
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            builderFactory.setExpandEntityReferences(false);
            builderFactory.setValidating(false);
            builderFactory.setNamespaceAware(true);
            builderFactory.setIgnoringComments(true);
            builderFactory.setCoalescing(true);
            builderFactory.setIgnoringElementContentWhitespace(true);

            return builderFactory.newDocumentBuilder().parse(in);
        } catch (ParserConfigurationException errParser) {
            throw new RuntimeException("Error getting XML parser", errParser);
        } catch (SAXException errSax) {
            throw new RuntimeException("Error parsing XML files", errSax);
        } catch (IOException errIO) {
            throw new RuntimeException("Error parsing XML files", errIO);
        }
    }

    public static Document parseStreamToXML(Reader in) {
        try {
            // Use JAXP to create a document builder
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            builderFactory.setExpandEntityReferences(false);
            builderFactory.setValidating(false);
            builderFactory.setNamespaceAware(true);
            builderFactory.setIgnoringComments(true);
            builderFactory.setCoalescing(true);
            builderFactory.setIgnoringElementContentWhitespace(true);

            return builderFactory.newDocumentBuilder().parse(new InputSource(in));
        } catch (ParserConfigurationException errParser) {
            throw new RuntimeException("Error getting XML parser", errParser);
        } catch (SAXException errSax) {
            throw new RuntimeException("Error parsing XML files", errSax);
        } catch (IOException errIO) {
            throw new RuntimeException("Error parsing XML files", errIO);
        }
    }
}

Related

  1. parseNamespace(XMLStreamReader reader)
  2. parseNoContent(final XMLStreamReader reader)
  3. parseNumber(XMLEventReader stream)
  4. parser(InputStream inputStream)
  5. parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)
  6. parseText(XMLStreamReader parser)
  7. parseValue(XMLStreamReader xmlRdr, String elementName)
  8. parseXml(InputStream in)
  9. parseXml(InputStream in)