What I Learned About XML Today

Dec 4th, 2007No Comments

Unlike most people, I tend to enjoy the days that I get up to my elbows in XML. It’s familiar to me and, let’s face it, there are much better tools today then there were seven or eight years ago when I first starting working with XML content.

Today was the day that I decided that a pure XPath solution was the best — I just needed one value out of a a document around 500Kb. No sense in parsing the whole thing into a DOM, or wrestling with StaX or even putting it into a whole XMLBeans structure.

So it was just me, the markup, and[ Java 5.](http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/package-summary.html) And these are the things that I learned:

* You can’t set a default namespace in XPath. If you have an XML document with one, you still have togive it a namespace prefix and a full URI. And, to boot, you have to implement [`NamespaceContext`](http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/namespace/NamespaceContext.html) in a new object and put that prefix and namespace in that object. Yep, more work than it should be.

* I’m a big fan of the tools that they give you with [`libxml`](http://xmlsoft.org/), particularly with [`xmllint`](http://xmlsoft.org/xmllint.html). But did you know `xmllint` has a command line interface that you can type in XPath calls? Very cool indeed! It goes like this:

% xmllint –shell computerbooks.xml
/ > ls //book[publisher="O'Reilly"]
ta- 3
— 1 title
ta- 3
— 1 publisher
ta- 1
ta- 3
— 1 title

Typing “help” will give you the useful stuff that you need.

Leave a Reply

You must be logged in to post a comment.