Java XML Document Create toDocument(Reader reader)

Here you can find the source of toDocument(Reader reader)

Description

Parses an XML document from a reader.

License

Open Source License

Parameter

Parameter Description
reader the reader

Exception

Parameter Description
SAXException if the XML is not valid
IOException if there is a problem reading from the reader

Return

the parsed DOM

Declaration

public static Document toDocument(Reader reader) throws SAXException, IOException 

Method Source Code


//package com.java2s;
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 {
    /**/*from  www.  j  a v  a2s. c o m*/
     * Parses an XML string into a DOM.
     * @param xml the XML string
     * @return the parsed DOM
     * @throws SAXException if the string is not valid XML
     */
    public static Document toDocument(String xml) throws SAXException {
        try {
            return toDocument(new StringReader(xml));
        } catch (IOException e) {
            //reading from string
            throw new RuntimeException(e);
        }
    }

    /**
     * Parses an XML document from an input stream.
     * @param in the input stream
     * @return the parsed DOM
     * @throws SAXException if the XML is not valid
     * @throws IOException if there is a problem reading from the input stream
     */
    public static Document toDocument(InputStream in) throws SAXException, IOException {
        return toDocument(new InputSource(in));
    }

    /**
     * <p>
     * Parses an XML document from a reader.
     * </p>
     * <p>
     * Note that use of this method is discouraged. It ignores the character
     * encoding that is defined within the XML document itself, and should only
     * be used if the encoding is undefined or if the encoding needs to be
     * ignored for whatever reason. The {@link #toDocument(InputStream)} method
     * should be used instead, since it takes the XML document's character
     * encoding into account when parsing.
     * </p>
     * @param reader the reader
     * @return the parsed DOM
     * @throws SAXException if the XML is not valid
     * @throws IOException if there is a problem reading from the reader
     * @see <a
     * href="http://stackoverflow.com/q/3482494/13379">http://stackoverflow.com/q/3482494/13379</a>
     */
    public static Document toDocument(Reader reader) throws SAXException, IOException {
        return toDocument(new InputSource(reader));
    }

    private static Document toDocument(InputSource in) throws SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setIgnoringComments(true);

        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            //will probably never be thrown because we're not doing anything fancy with the configuration
            throw new RuntimeException(e);
        }

        return builder.parse(in);
    }
}

Related

  1. stringToDocument(String string)
  2. stringToDocument(String xml)
  3. stringToDocument(String xmlStr, boolean isValidating)
  4. stringToDocument(String xmlString)
  5. toDocument(File f)
  6. toDocument(ResultSet rs)
  7. toDocument(SOAPMessage soapMsg)
  8. toDomDocument(Node domNode)