Using XPath in Java without loading the external DTD
Java is a complicated mess. I just wasted 3 hours figuring out how to use XPath expressions in Java 1.5 without loading the external DTD. Thanks to the absence of any worthy documentation, I had to guess, and guess again, until I came up with this.
DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
dbfact.setAttribute(“http: // apache.org / xml / features / nonvalidating / load-external-dtd”,false);
DocumentBuilder builder = dbfact.newDocumentBuilder();
Document indexname_input = builder.parse(someinputstream);
XPathFactory fact = XPathFactory.newInstance();
XPath xpath = fact.newXPath();
String title = xpath.evaluate(“string(//frontmatter/titlepage/title)”, indexname_input);
Montreal, Canada 
Follow on
Read the API: you just need an
dbfact.setValidating(false);
Comment by Anonymous — 14/6/2005 @ 9:08
Whether or not you validate is not the same as loading the external DTD or not. Doing as you suggest will still result in Java trying to load the external DTD.
Comment by Daniel Lemire — 14/6/2005 @ 12:03