Phillip Pearson - web + electronics notes

tech notes and web hackery from a new zealander who was vaguely useful on the web back in 2002 (see: python community server, the blogging ecosystem, the new zealand coffee review, the internet topic exchange).


Turned off the Google Toolbar

I just realised that, since installing IE7, I haven't used the Google Toolbar -- IE7's Firefox-like search box is all I need. So I've turned it off.

Using XPath with namespaced documents in dom4j

In dom4j, once you have parsed a document, you can execute XPath expressions like so:

List list = doc.selectNodes("/path/to/elements/you/want");

The doc.selectNodes function is a thin wrapper around the jaxen XPath library. This saves you from having to write:

XPath xp = new org.jaxen.dom.Dom4jXPath("/path/to/elements/you/want");
List list = xp.execute(doc);

However, if the document uses namespaces, you have to tell jaxen about them, or you won't have any way of specifying namespaced elements. Until yesterday, I thought the only way to do this was:

XPath xp = new org.jaxen.dom.Dom4jXPath("/x:path/x:to/x:elements/x:you/x:want");
xp.addNamespace("x", "");
List list = xp.execute(doc);

This is a pain in the ass - three lines of code every time you want to use XPath! I did it this way for a little while, but finally got sick of it yesterday and dug through the dom4j code until eventually finding out how to do it properly:

Map namespaces = new TreeMap();
namespaces.put("x", "");

From then on, any time you call selectNodes on a dom4j Node object, you'll be able to use the x: namespace prefix. Much nicer!

... more like this: []