Java XML Parse Stream parse(InputStream byteStream)

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

Description

Parses the input stream and returns a DOM Document .

License

Open Source License

Parameter

Parameter Description
byteStream The input stream.

Return

The parsed from the input.

Declaration

public static Document parse(InputStream byteStream) 

Method Source Code


//package com.java2s;
import java.io.InputStream;

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;

import org.w3c.dom.ls.LSParser;

public class Main {
    /**/*from  w  ww. j a va2  s  .  co m*/
     * Parses the input stream and returns a DOM {@link Document}.
     * 
     * @param byteStream
     *            The input stream.
     * @return The {@link Document} parsed from the input.
     */
    public static Document parse(InputStream byteStream) {
        // input
        DOMImplementationLS impl = (DOMImplementationLS) getDOMImplementation();
        LSInput input = impl.createLSInput();
        input.setByteStream(byteStream);

        // parse without comments and whitespace
        LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
        DOMConfiguration config = builder.getDomConfig();
        config.setParameter("comments", false);
        config.setParameter("element-content-whitespace", false);

        // returns the document parsed from the input
        return builder.parse(input);
    }

    /**
     * Returns a DOM implementation that has the following features:
     * <ul>
     * <li>Core 3.0</li>
     * <li>XML 3.0</li>
     * <li>LS</li>
     * </ul>
     * 
     * @return A {@link DOMImplementation} object that can be cast to
     *         {@link DOMImplementationLS}.
     */
    public static DOMImplementation getDOMImplementation() {
        try {
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            return registry.getDOMImplementation("Core 3.0 XML 3.0 LS");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. parse(DefaultHandler handler, InputStream in)
  2. parse(final InputStream in)
  3. parse(final InputStream inputStream)
  4. parse(final InputStream is)
  5. parse(InputStream anInputStream)
  6. parse(InputStream in)
  7. parse(InputStream in)
  8. parse(InputStream inputStream)
  9. parse(InputStream inputStream)