Java tutorial
//package com.java2s; import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { /** Read an XML file into a DOM tree * * @param file the file to read * @return a new XML document * @throws SAXException if an XML parse error occurs * @throws IOException if a file I/O error occurs */ public static Document read(File file) throws SAXException, IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Workaround for missing external DTD. When the parser looks up // an external reference, we provide an empty document back. While // not correct, we don't expect SVG files loaded by this program // to depend on externally defined entities; nor do we require // validation. // // http://stackoverflow.com/questions/2640825/how-to-parse-a-xhtml-ignoring-the-doctype-declaration-using-dom-parser builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); return builder.parse(file); } catch (ParserConfigurationException e) { // This exception is never thrown, treat as fatal if it is throw new RuntimeException(e); } } }