Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.net.URI; public class Main { public static Document parse(File file) throws IOException { try { return createDocumentBuilder().parse(file); } catch (Exception e) { throw new IOException(e); } } public static Document parse(InputStream input) throws IOException { try { return createDocumentBuilder().parse(input); } catch (Exception e) { throw new IOException(e); } } public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputStream source = !systemId.startsWith("file:") ? null : getClass().getResourceAsStream( "/net/sf/logsupport/" + new File(URI.create(systemId)).getName()); return source == null ? new InputSource(new StringReader("")) : new InputSource(source); } }); return builder; } }