Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Nonhierarchical Nodes

There are a few structural Infoset properties that do not fit into the [parent]/[children] relationships of the Infoset. These include the [attributes] property of an element information item as well as the [notations] and [entities] properties of a document information item. Each of these properties exposes a collection of information items as an unordered collection that is accessed by name. To model this sort of collection, the DOM defines the NamedNodeMap interface.

interface NamedNodeMap {
  Node item(in unsigned long index);
  readonly attribute unsigned long    length;
  Node getNamedItemNS(in DOMString namespaceURI,
                      in DOMString localName);
  Node getNamedItem(in DOMString name);
  Node setNamedItemNS(in Node arg) raises(DOMException);
  Node setNamedItem(in Node arg)   raises(DOMException);
  Node removeNamedItemNS(in DOMString namespaceURI,
                         in DOMString localName)
  Node removeNamedItem(in DOMString name)

Note that this interface provides both positional and named access via its item and getNamedItem methods, respectively. The NamedNodeMap interface is used to access attribute, entity, and notation information items. Since attribute information items use namespaces, but entity and notation information items do not, the three "NamedItem" methods come in two forms—one that works in terms of namespace URI + local name and one that works in terms of just name. The former style is for attributes, and the latter form is for entities and notations.

The [attributes] Infoset property is exposed via the attributes attribute on the Node interface.

interface Node {
  readonly attribute NamedNodeMap attributes;
    :         :          :               :

The following code adds an attribute to the provided element:

import org.w3c.dom.*;
void annotate(Node elem) {
  Document document = elem.getOwnerDocument();
  Node attr = document.createAttributeNS(
                      "http://foo.com/names", "note");
  attr.setNodeValue("Hello, world");
  NamedNodeMap attrs = elem.getAttributes();

Because the attributes attribute is only valid for Element nodes, this code will fail miserably for non-Element nodes.

The [notations] and [entities] Infoset properties are exposed via the notations and entities attributes of the DocumentType interface.

interface DocumentType : Node {
  readonly attribute NamedNodeMap entities;
  readonly attribute NamedNodeMap notations;

The DocumentType node is available as an attribute of the Document interface.

interface Document : Node {
  readonly attribute DocumentType doctype;
    :          :            :        :

Given these interface definitions, the following Java code returns the entity named bob that is related to a node's document:

import org.w3c.dom.*;
Node getBob(Node someNode) {
  Document document = someNode.getOwnerDocument();
  DocumentType doctype = document.getDoctype();
  if (doctype == null) return null;
  NamedNodeMap entities = doctype.getEntities();
  return entities.getNamedItem("bob");

Note that the node returned by this function also implements the Entity interface to provide access to the [system identifier], [public identifier], and [notation] Infoset properties.

  • + Share This
  • 🔖 Save To Your Account