get XML Document From Class Path - Java XML

Java examples for XML:DOM Document

Description

get XML Document From Class Path

Demo Code


import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class Main{
    public static void main(String[] argv) throws Exception{
        String resourceName = "java2s.com";
        System.out.println(getDocumentFromClassPath(resourceName));
    }/* w  w  w  .j  a  va 2s. co  m*/
    public static Document getDocumentFromClassPath(String resourceName) {
        return getDocumentFromClassPath(resourceName, true);
    }
    public static Document getDocumentFromClassPath(String resourceName,
            boolean namespaceAware) {
        return getDocumentFromStream(ResourceUtil.getStream(resourceName),
                namespaceAware);
    }
    public static Document getDocumentFromStream(InputStream is,
            boolean namespaceAware) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            dbf.setNamespaceAware(namespaceAware);
            DocumentBuilder builder = dbf.newDocumentBuilder();
            Document ret = builder.parse(is);
            return ret;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials