Re: XML Paths in Programming Languages

The other big different between XPaths and path-based syntax in languages is what happens when a particular element doesn’t exist. For example, consider the XPath “/doc/house/floor/room”. The return value is a null if any of <doc>, <house>, <floor>, or <room> doesn’t exist in the document (or if the path in question doesn’t occur in the document). The programming language construct “doc.house.floor.room” throws a null pointer exception if “doc” or “doc.house” or “doc.house.floor” evaluates to null.

That makes a very big difference to your exception handling logic/code. You can just catch the exception each time (at some CPU cost which may or may not be significant), which means wrapping a try/catch construct around every path statement. That makes the code ugly and hard to read, unfortunately. So it turns out to be a big difference in practice.

Cheers, Tony.

Reply

The content of this field is kept private and will not be shown publicly.