Monday, May 23rd, 2005

Using XPath in Java without loading the external DTD

Filed under: — lemire @ 22:07

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

RSS feed for comments on this post.

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: I + II + IX= XII. Yes, you have to enter a roman numeral. (Answer must be in upper case.)

« Blog's main page

25 queries. 0.585 seconds. Valid XHTML

Powered by WordPress

Subscribe to this blog in a reader or by Email.