Techie:Techie Main/Java/XML Parsing/Prevent DTD download: Difference between revisions
Created page with 'When parsing an XML document, here's how to create a DocumentBuilder which won't insist on downloading DTDs referenced in the documents it parses private static DocumentBuil…' |
No edit summary |
||
Line 9: | Line 9: | ||
throw new RuntimeException("Failed to create DocumentBuilder", e); | throw new RuntimeException("Failed to create DocumentBuilder", e); | ||
} | } | ||
// required to stop the parser trying to download DTDs | // required to stop the parser trying to download DTDs | ||
EntityResolver resolver = new DefaultHandler(){ | EntityResolver resolver = new DefaultHandler(){ |
Latest revision as of 14:33, 11 June 2010
When parsing an XML document, here's how to create a DocumentBuilder which won't insist on downloading DTDs referenced in the documents it parses
private static DocumentBuilder documentBuilder; static { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { documentBuilder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException("Failed to create DocumentBuilder", e); } // required to stop the parser trying to download DTDs EntityResolver resolver = new DefaultHandler(){ public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) throws org.xml.sax.SAXException, java.io.IOException { return new org.xml.sax.InputSource(new java.io.StringReader("")); } }; documentBuilder.setEntityResolver(resolver); }