Ronan’s Blog


Random thoughts of an Irish Software Architect in the GIS Industry.

Parsing XML without an Internet connection

Posted in Development, Java, Web by Ro on the May 1st, 2007

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);

Leave a Reply