In the last two tips, I showed you pretty much everything you need to know to bootstrap under DOM Levels 1 and 2. In addition to the basic concepts involved in bootstrapping, you should know why you'd want to work with the DOMImplementation class and how DOM Levels 1 and 2 can easily tie you to a specific vendor's DOM implementation. Further, in the last tip, I showed you how a simple utility class can make it much easier to handle parser changes.
While all that is well and good, it still requires a lot of pain and effort (or at least a few google.com searches) to do something that really should be pretty simple. Why in the world doesn't the DOM specification address these difficulties of bootstrapping? Well, I'm happy to report that in DOM Level 3, bootstrapping is finally examined and reworked in improved, vendor-neutral ways.
Note: Before going any further, I want to emphasize that DOM Level 3 is still on the horizon, and you will be hard-pressed to find parsers that completely support it. That said, it is coming, and once it's finalized, you can take advantage of all its new features, including the bootstrapping discussed here.
DOM Level 3 introduces a new means of bootstrapping, one that avoids the nasty vendor-specific problems found in its predecessors. It also avoids the need for a helper class, as designed in the last tip. Through the introduction of a new DOM class, org.w3c.dom.DOMImplementationRegistry, it is possible to get a DOM implementation in a vendor-neutral way.
First, you (or your parser vendor) need to set the system property org.w3c.dom.DOMImplementationSourceList. This property's value should be a space-separated list of class names that implement the org.w3c.dom.DOMImplementationSource interface. This is the key mechanism for DOM-implementing parsers. For example, Listing 1 shows how the Apache Xerces parser might implement this interface.
package org.apache.xerces.dom;
import org.w3c.dom.DOMImplementationSource;
public class XercesDOMImplementationSource implements DOMImplementationSource {
public DOMImplementation getDOMImplementation(String features) {
return new DOMImplementationImpl();
}
}
|
Note: This is not the actual Xerces implementation class; in reality, the getDOMImplementation() method needs to verify the feature string, ensure that the Xerces implementation was sufficient, and perform other error checking before returning a DOMImplementation implementation.
You can then set the system property to the value org.apache.xerces.dom.XercesDOMImplementationSource. Typically, this property is set through the parser's own code, or at the startup of your application through a batch file or shell script, as seen in Listing 2.
java -Dorg.w3c.dom.DOMImplementationSourceList\ =org.apache.xerces.dom.XercesDOMImplementationSource \ some.application.class |
With this machinery in place, you can then easily bootstrap a DOM implementation, using the line of code shown in Listing 3.
DOMImplementation domImpl =
DOMImplementationRegistry.getDOMImplementation("XML 1.0");
|
From there, it is simple to create a new DOM tree and perform other standard DOM operations. Because the system property handles loading the parser- and vendor-specific details, your code remains free of vendor-specific idioms.
So there you have it -- three tips that have exhausted (in more ways that one!) the subject of DOM bootstrapping. I'm thrilled to report that I'll move on now and get into some more interesting programming tasks. So, until the next tip, I'll see you online!
-
Part 1 of this series of tips on bootstrapping with DOM explains what bootstrapping is, explores the problems associated with it, and lays the basics for use in DOM Levels 1 and 2 (developerWorks, November 2002).
Part 2 builds upon its predecessor by showing you a better way to bootstrap in your DOM applications (developerWorks, December 2002).
- Read about the DOM API on W3C.org.
- Learn about the structure of a DOM document, and how to use Java technology to create a document from an XML file, make changes to it, and retrieve output, in Nicholas Chase's tutorial "Understanding DOM" (developerWorks, August 2001).
- Find more XML resources on the developerWorks XML zone.
-
IBM trial software: Build your next development project with trial software available for download directly from developerWorks.
- Find out how you can become an IBM Certified Developer in XML and related technologies.
- Want us to send you useful XML tips like this every week? Sign up for the developerWorks XML Tips newsletter.

Brett McLaughlin has been working in computers since the Logo days (remember the little triangle?). He currently specializes in building application infrastructure using the Java language and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.