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

2 Comments »

  1. Read the API: you just need an

    dbfact.setValidating(false);

    Comment by Anonymous — 14/6/2005 @ 9:08

  2. 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

Leave a comment

Warning: When entering a long comment, please ensure that you make copy of your text prior to submitting it. If the server should fail or if you hit a bug, you might lose your work. I am not responsible for your lost effort.

To spammers: I carefully review every single post and make sure that spam gets deleted. You are wasting your time if you are manually entering spam using this form. Read my terms of use to see what I consider to be abusive.

Example: duo plus septem is '9'. The numbers are expressed in latin numerals but you should give your answers using ordinary digits.

 

« Blog's main page

Powered by WordPress