DOM Interfaces

Let’s examine in more detail the interfaces in Figure 16.1.

The Node Interface

As you saw in Figure 16.1, many DOM nodes implement the Node interface. They might also have other properties and methods in addition to those that the Node interface provides.

These DOM interfaces are said to extend the Node interface. Figure 16.2 shows the interfaces in DOM Level 2 Core that extend the Node interface.

Figure 16.2. The DOM Level 2 interfaces that extend the Node interface.


In general, the names of the node types that inherit from the Node interface can be readily understood from their names. The DocumentType interface, for example, corresponds to the DOCTYPE declaration.

DOM interfaces can be extended several levels deep. Figure 16.3 shows the node types that extend the CharacterData interface.

Figure 16.3. The interfaces that extend the CharacterData interface.


Both the Comment and Text interfaces extend the CharacterData interface. The Text interface itself is extended by the CDATASection interface. You will look at these text-oriented interfaces later, but first let’s look briefly at the properties and methods of the Node interface.

The Node interface has the following properties:

  • attributes Read-only, of type NamedNodeList

  • childNodes— Read-only, of type NodeList

  • firstChild— Read-only, of type Node

  • lastChild— Read-only, of type Node

  • localName— Read-only, of type String

  • namespaceURI— Read-only, of type String

  • nextSibling— Read-only, of type Node

  • nodeName— Read-only, of type String

  • nodeType— Read-only, of type Number

  • nodeValue— Of type String

  • ownerDocument— Read-only, of type Document

  • parentNode— Read-only, of type Node

  • prefix— Of type String

  • previousSibling— Read-only, of type Node

The Node interface has the following methods:

  • appendChild(newChild)— Returns a Node object

  • cloneNode(deep)— Returns a Node object

  • hasAttributes()— Returns a Boolean value

  • hasChildNodes()— Returns a Boolean value

  • insertBefore(newChild, refChild)— Returns a Node object

  • isSupported(feature, value)— Returns a Boolean value

  • normalize()— Has no return value

  • replaceChild(newChild, oldChild)— Returns a Node object

  • removeChild(oldChild)— Returns a Node object

The NodeList Interface

The child nodes of the Document node implement the NodeList interface.

The NodeList interface has the length property, which is a read-only property of type Number. The NodeList interface has the item(index) method, which returns a Node object.

The NamedNodeMap Interface

You can locate the attributes of an element using the NamedNodeMap interface.

The NamedNodeMap interface has the length property, a read-only property of type Number.

The NamedNodeMap interface has the following methods, all of which return a Node object:

  • getNamedItem(name )

  • getNamedItemNS(namespaceURI ,localName )

  • item(index)

  • removeNamedItem(name )

  • removeNamedItemNS(namespaceURI ,localName )

  • setNamedItem(arg )

  • setNamedItemNS(namespaceURI ,localName )

Before looking in more detail at the interfaces in Figure 16.1, you will look briefly at generally relevant interfaces.

The DOMImplementation Interface

The DOMImplementation interface has no properties, but it does provide methods that allow a programmer to determine what is supported by that implementation.

The hasFeature(feature , version ) method returns a Boolean value enabling you to test the availability of particular features. The createDocument(namespaceURI , qualifiedName , doctype ) method enables you to create a new XML document. The createDocumentType(qualifiedName , publicId , systemId ) method enables you to create a new DOCTYPE declaration.

The DOMException Interface

Several programming languages use a concept called an exception to handle errors that occur while a program is running. The DOM provides a DOMException interface. An exception is often said to be thrown and is caught by an exception handler.

The DOMException interface has a single code property, which is of type number. The value of the code property corresponds to the type of exception that has been raised. When programming, you should consider the likely types of errors that might occur and provide error-handling code that provides appropriate responses to those problems.

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

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