The Document Object Model APIs
Figure 4-2 shows the DOM APIs in action.
You use the
javax.xml.parsers.DocumentBuilderFactory
class to get aDocumentBuilder
instance, and you use that instance to produce aDocument
object that conforms to the DOM specification. The builder you get, in fact, is determined by the system propertyjavax.xml.parsers.DocumentBuilderFactory
, which selects the factory implementation that is used to produce the builder. (The platform's default value can be overridden from the command line.)You can also use the
DocumentBuilder
newDocument()
method to create an emptyDocument
that implements theorg.w3c.dom.Document
interface. Alternatively, you can use one of the builder's parse methods to create aDocument
from existing XML data. The result is a DOM tree like that shown in Figure 4-2.
Note: Although they are called objects, the entries in the DOM tree are actually fairly low-level data structures. For example, consider this structure:
<color>blue</color>
. There is an element node for thecolor
tag, and under that there is a text node that contains the data,blue
! This issue will be explored at length in the DOM section of the tutorial, but developers who are expecting objects are usually surprised to find that invokinggetNodeValue()
on the element node returns nothing! For a truly object-oriented tree, see the JDOM API athttp://www.jdom.org
.
The DOM Packages
The Document Object Model implementation is defined in the packages listed in Table 4-2.