Bootstrap DOM Correctly

One of the most misunderstood and abused aspects of using DOM is getting an initial DOM implementation for programming. This is typically referred to as bootstrapping, and it is often done incorrectly. There are two different approaches, depending on which version of the DOM specification your implementation adheres to.

DOM Levels 1 and 2

In DOM Levels 1 and 2, the process of getting a DOM implementation to work is difficult. Before I explain why, though, you should understand why you need a DOM implementation in the first place.

Tip

If you are reading in an XML document (e.g., from an existing file or input stream), this entire section is not applicable. In these cases, the reader.getDocument( ) method will return a DOM Document object, and you can then operate on that DOM tree without any problem. I’m also assuming that you’ve chosen not to use JAXP. If you are using JAXP, these issues are taken care of for you. In many cases, however, JAXP either is not available or you have software restrictions that make it a nonoption in your application or organization.

The end goal of bootstrapping is to get a vendor’s implementation of the org.w3c.dom.Document interface. Most developers’ instincts, therefore, are to write this line of code:

Document doc = new org.apache.xerces.dom.DocumentImpl(  );

The obvious problem here is that your code is now tied to Xerces, and can’t work with another parser without modification. In fact, it often won’t function with a different ...

Get Java Enterprise Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.