Java XML Parse Stream parse(InputStream s, boolean validateIfSchema)

Here you can find the source of parse(InputStream s, boolean validateIfSchema)

Description

parse

License

Apache License

Declaration

public static Document parse(InputStream s, boolean validateIfSchema) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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 {
    private static DOMImplementation IMPL;

    public static Document parse(String s) {
        return parse(s, false);
    }/*from ww  w. j  a  v  a 2 s  .  c  o m*/

    public static Document parse(String s, boolean validateIfSchema) {
        DOMImplementationLS impl = getDOMImpl();
        LSInput input = impl.createLSInput();
        input.setStringData(s);
        return parse(input, validateIfSchema);
    }

    public static Document parse(InputStream s, boolean validateIfSchema) {
        DOMImplementationLS impl = getDOMImpl();
        LSInput input = impl.createLSInput();
        input.setByteStream(s);
        return parse(input, validateIfSchema);
    }

    private static Document parse(LSInput input, boolean validateIfSchema) {
        DOMImplementationLS impl = getDOMImpl();
        LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
        DOMConfiguration config = parser.getDomConfig();
        config.setParameter("element-content-whitespace", false);
        config.setParameter("namespaces", true);
        config.setParameter("validate-if-schema", validateIfSchema);
        return parser.parse(input);
    }

    @SuppressWarnings("unchecked")
    public synchronized static <T> T getDOMImpl() {
        if (IMPL == null) {
            try {
                DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
                IMPL = registry.getDOMImplementation("LS 3.0");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return (T) IMPL;
    }
}

Related

  1. parse(InputStream is)
  2. parse(InputStream is)
  3. parse(InputStream is)
  4. parse(InputStream is)
  5. parse(InputStream is)
  6. parse(InputStream stream)
  7. parse(XMLStreamReader reader)
  8. parse(XMLStreamReader reader, String... elementNames)
  9. parseDoc(final InputStream is)