XPath using Dom4J

Posted: 6 June 2011 in Uncategorized

A while back I wrote a post on how to do XPath in a w3c context. Here’s an equivalent example using dom4j. I’ve even added a little thingy for handling a default namespace (commented out since the original example was for a DCR.

HashMap namespaces = new HashMap();
namespaces.put("html", "http://www.w3.org/1999/xhtml"); // When we have a doctype, html has a default namespace which is fixed 
String titleXPathExpression = /article/title";
if (titleXPathExpression != null && !titleXPathExpression.equals("")) {
     XPath titleXPath = new org.dom4j.xpath.DefaultXPath(titleXPathExpression);
     // titleXPath.setNamespaceURIs(namespaces);
     String title = titleXPath.valueOf(dom4jHowToXMLDocument);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s