Parsing XML without an Internet connection
I came across this hand bit of Java to allow you to parse XML while behind a restrictive firewall or without any internet connection at all. Previously, reading the DTD was causing java.net.UnknownHostException’s, the updated code stops the parser from trying to connect to the Internet to download the DTD.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//Here is the new code….
docBuilder.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
return new InputSource(
new ByteArrayInputStream( ““.getBytes()));
}
} );
document = docBuilder.parse(xmlFile);