Java XML Parse String parse(String s)

Here you can find the source of parse(String s)

Description

parse

License

Apache License

Declaration

public static Document parse(String s) 

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);
    }/* w  w w.  ja v a2 s.co 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(final String xmlContent)
  2. parse(String input)
  3. parse(String input, char delim, char esc)
  4. parse(String line)
  5. parse(String line)
  6. parse(String text)
  7. parse(String xml)
  8. parse(String xml)
  9. parse(String xml)