The Document Object Model

The Document Object Model (DOM) is a series of W3C specifications that provide increasing functionality to access and manipulate XML (and HTML) documents programmatically. The DOM provides a practical way to manipulate, create, and modify XML documents programmatically.

At the time of this writing, the DOM Level 2 specifications (see Appendix B, “XML Tools,” for links) are the current versions. DOM Level 3 specifications are in development.

Note

Only the DOM Level 2 Core interfaces are considered in this chapter and in Chapter 17, “The Document Object Model—2.” The Document Object Model Level 2 also specifies more specialized interfaces to be used—for example, with HTML documents and CSS style sheets.



Object and Interfaces

The name of the Document Object Model refers to objects, but the DOM is defined in terms of interfaces. An object packages a specified group of properties and methods in a convenient object, for want of a better term. A property can be thought of as a characteristic of an object. For example, a car might have a color property and a number_of_wheels property. Each of these properties tells about some characteristic of the car. Similarly, a car object might have go_forward(), go_backwards(), and stop() methods. These methods would tell something about what the object can do.

An interface can be thought of as a convenient package of properties and methods. An object can implement an interface—a specified and named package of properties and methods. It can either add properties and methods specific to that object or can implement the properties and methods defined in one or more other interfaces.

You learned earlier that an XML document can be viewed as a logical hierarchy. In the DOM, you can model that hierarchy using several nodes.

Let’s look at a simple XML document and consider how it is represented in the DOM. Figure 16.1 shows a hierarchy representing the interfaces and objects that make up the DOM representation of the document.

Figure 16.1. A hierarchical representation of the example document.


<book edition="1"> 
Sams Teach Yourself XML in 10 Minutes 
</book> 

Let’s first look at Figure 16.1 and consider each part of the hierarchy. Later you will look at individual interfaces shown in the figure in more detail.

The node at the apex of the hierarchy represents the (invisible) document entity of an XML document. That node implements the Document interface and the Node interface.

The next node in the hierarchy implements the NodeList interface. In this simple document there is only a single node that is the child of the NodeList—the node representing the document element, the book element, of the XML document. That node implements the Node interface and the Element interface.

The Element node has a child node that implements the NamedNodeList interface as its child node. That node has a single child node that represents the edition attribute in the XML document. The node implements the Attr and Node interfaces.

That Element node also has a node that implements the NodeList interface as a child node. In this simple document, there is only a single node that contains the text content of the Book element node. That node implements the Node, Text, and CharacterData interfaces.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset