XML

DOM Interfaces

As I've already mentioned, in order to comply with the DOM, an implementation must include the interfaces required by the DOM specification. Each of the interfaces is associated with a particular type of node found in a DOM tree. Rather than listing and describing all of these interfaces, I'm going to focus on the interfaces that you will use most frequently in the course of writing applications that use the DOM. Later in the lesson you'll work through a sample application that uses several of these interfaces.

The Node Interface

The Node interface is the interface from which all other interfaces are derived. Regardless of whatever else a particular entity in a DOM tree is, it's still a node. The Node interface exposes some attributes and methods that are shared by everything that's in a DOM tree. These attributes and methods are largely associated with keeping track of the parents, siblings, and children of the node. Nodes also have attributes that contain their names, values, and a pointer to the document with which they are associated.

The Document Interface

The Document interface is the root node of a DOM tree. There's one important thing to point out here: the document node in a DOM tree is not the root element of the XML documentit's one level above that. Every document has one child element node that contains the root element of the XML document. Document nodes have other attributes that are associated with the document itself, rather than with the root element. That's why a DOM tree has one more level in its hierarchy than an XML document.

In addition to the root level element of the document, the document node also contains a pointer to a DocumentType node and a DOMImplementation node. Neither of these nodes are commonly referenced on programs which use the DOM, so I'm not going to discuss them individually. For more information, check out the DOM specification at http://www.w3.org/DOM/DOMTR.

The Element Interface

The Element interface represents an element in an XML (or HTML) document. The only attribute specific to an element node is the tag name (of course, it inherits all of the attributes of a standard node as well). It includes methods that enable you to retrieve, add, and remove attributes from the element. It also enables you to retrieve child elements that have a specific name. (You can already fetch all of the children of an element using the methods it inherits from the Node interface.)

The Attr Interface

The Attr interface represents an attribute of an element. Despite the fact that they inherit the Node interface, they are not actually nodes on the DOM tree because they are not children of an element. Rather, they are part of the element itself. What this means in practical terms is that any methods inherited from the Node interface that deal with traversing the DOM tree return null (nothing). You can't fetch the parents, siblings, or children of an attribute, because in terms of the DOM, they don't have any of those things.

Attributes have three propertiesa name, a value, and a Boolean flag indicating whether or not the attribute was explicitly included in the document that was parsed. They have no methods specific to themselves.

The NodeList Interface

The NodeList interface is different from the other interfaces I've discussed so far. It's not a pointer to an entity in a DOM tree; rather, it's an abstract data structure that enables DOM implementations to handle collections of nodes. For example, if you call the method of the Element interface that returns all the children of an element with a particular name, the collection of child elements is returned as a node list (NodeList). You can then iterate over the node list and extract all of the element nodes from it. The only interface it must implement is one that returns items by index. Its only attribute is the size of the collection. Using the size and the method, which returns items, you can iterate over the members of the collection.

by BrainBellupdated
Advertisement: