XML technologies / XML / XML in a Nutshell / The Document Object Model (DOM) / Specialized node types

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Although the Node interface already provides access to individual XML document parts, the DOM core offers more specialized interfaces to simplify common programming tasks. These fall broadly into structural and content-oriented node types.

  A document in memory with links

Figure: The document in memory with links

Structural node types

XML contains syntactic structures not formally considered content. These interfaces represent document parts containing no element data.

DocumentType

The interface DocumentType provides access to the notations introduced in the document type definition, to entities, PUBLIC and SYSTEM identifiers, and local grammar rules. Only one !DOCTYPE declaration is allowed per document, so each DOM document may contain just one DocumentType object. Obtain it through the attribute doctype in the interface Document. The following table defines the DocumentType interface.

Table: The DocumentType interface derived from Node

Name

Type

Read-only?

Attribute

entities

NamedNodeMap

✗

internalSubset

DOMString

✗

name

DOMString

✗

notations

NamedNodeMap

✗

publicId

DOMString

✗

systemId

DOMString

✗

With the extra fields available since DOM Level 2, a parsed document can now be fully reconstructed from DOM framework information alone. Currently there is no programmatic way to modify the content of DocumentType nodes.

ProcessingInstruction

The node type ProcessingInstruction directly accesses the content of a processing instruction. Although processing instructions usually appear in document text, they may also occur before or after the root element and in DTDs. The following table describes the attributes of the ProcessingInstruction.

Table: The ProcessingInstruction interface derived from Node

Name

Type

Read-only?

Attribute

data

DOMString

target

DOMString

✗

Remember that only the target has a specified syntax, that of an XML token. The remaining data may take almost any form up to the closing >. For more on using and potentially misusing processing instructions, see Programming models.

Notation

XML notations formalize format declarations for external parsed entities and processing instructions. The document’s notation list is a NamedNodeMap object in the DOCTYPE declaration, accessible through the interface Document. The following table defines the Notation interface.

Table: The Notation interface derived from Node

Name

Type

Read-only?

Attribute

publicId

DOMString

✗

systemId

DOMString

✗

Entity

The name of the interface Entity initially seems ambiguous, but becomes clearer when you know the DOM core also includes an EntityReference interface. The interface Entity accesses the notation name and PUBLIC and SYSTEM identifiers of the entity declaration. Parsed entity nodes have childNodes, while unparsed entities have a notationName. The following table defines this interface.

Table: The Entity interface derived from Node

Name

Type

Read-only?

2.0

3.0

Attribute

inputEncoding

DOMString

✗

notationName

DOMString

✗

publicId

DOMString

✗

systemId

DOMString

✗

xmlEncoding

DOMString

✗

✗

xmlVersion

DOMString

✗

✗

DOM Level 3 adds three attributes for external parsed entities: inputEncoding, xmlEncoding and xmlVersion. This information permits proper enforcement of well-formedness constraints based on the xmlVersion attribute’s value. The two encoding attributes permit accurate reconstruction of external parsed entity files from the DOM tree.

All attributes are read-only and cannot change at runtime.

Content-oriented node types

Actual XML data lies within the document element. These node types represent nonstructural parts such as character data, elements and attribute values.

 

Document

After parsing an XML document, the result in memory is one instance of Document. Empty Document nodes can be created through the DOMImplementation interface. It accesses the document type definition and the single top-level Element node containing the parsed document’s actual data, the documentElement. It also offers methods for applications to create new nodes not generated during parsing. The following table describes all attributes and methods of the Document.

Table: The Document interface derived from Node

Name

Type

Read-only?

2.0

3.0

Attribute

doctype

DocumentType

✗

documentElement

Element

✗

documentURI

DOMString

✗

domConfig

DOMConfiguration

✗

✗

implementation

DOMImplementation

✗

inputEncoding

DOMString

✗

✗

strictErrorChecking

boolean

✗

xmlEncoding

DOMString

✗

✗

xmlStandalone

boolean

✗

✗

xmlVersion

DOMString

✗

Methoden

adoptNode

Node

✗

createAttribute

Attr

createAttributeNS

Attr

✗

createCDATASection

CDATASection

createComment

Comment

createDocumentFragment

DocumentFragment

createElement

Element

createElementNS

Element

✗

createEntityReference

EntityReference

createProcessingInstruction

ProcessingInstruction

createTextNode

Text

getElementById

Element

✗

getElementsByTagName

NodeList

getElementsByTagNameNS

NodeList

✗

importNode

Node

✗

normalizeDocument

void

✗

renameNode

Node

✗

The various create...( ) methods matter when applications extend or modify a parsed document’s structure. Nodes created by one Document instance can only be inserted into the tree of the Document that created them. DOM Level 2 therefore introduced importNode( ) to copy a node’s content from one Document to another, optionally including children. DOM Level 3 introduced adoptNode() to move an entire subtree between documents.

Besides creation methods, there are methods for finding XML elements or lists. The methods getElementsByTagName( ) and getElementsByTagNameNS( ) list all XML elements with a particular name; the latter also restricts searches to a specified namespace. The method getElementById( ) finds the element with an XML attribute ID matching the supplied value.

DOM Level 3 also added attributes helping applications reconstruct XML as it existed before parsing. The attributes inputEncoding, xmlEncoding and xmlStandalone record values from the original document’s XML declaration and its character encoding before parsing and conversion to Unicode.

A major DOM Level 3 addition was document validation within the DOM tree itself. The method normalizeDocument( ) effectively lets developers reparse the XML document from the in-memory tree. The attribute domConfig exposes parameters controlling normalization. Changing the attribute xmlVersion beforehand can also change the target XML version, causing DOM to enforce the corresponding rules for XML names. For differences between XML 1.0 and 1.1, see the XML reference.

DocumentFragment

Applications editing existing documents sometimes need to keep nodes outside the parsed document hierarchy. For example, an XML editor may provide a clipboard. Cut nodes can temporarily move to a DocumentFragment node rather than remain in the source document, without being deleted. To paste them back, a method such as Node.appendChild() moves them back. The interface Node is the base of DocumentFragment, which has no specific attributes or methods.

Element

Nodes of type Element are the most commonly used parts of typical XML documents. They serve as parents for instances of Text, Comment, EntityReference, ProcessingInstruction, CDATASection and for child nodes of type Element forming the actual document content. They also access the Attr instances holding the element’s XML attributes. The following table lists all attributes and methods supported by the Element interface.

Table: The Element interface derived from Node

Name

Type

Read-only?

2.0

3.0

Attribute

schemaTypeInfo

TypeInfo

✗

✗

tagName

DOMString

✗

Methoden

getAttribute

DOMString

getAttributeNode

Attr

getAttributeNodeNS

Attr

✗

getAttributeNS

DOMString

✗

getElementsByTagName

NodeList

getElementsByTagNameNS

NodeList

✗

hasAttribute

boolean

✗

hasAttributeNS

boolean

✗

removeAttribute

void

removeAttributeNode

Attr

removeAttributeNS

Attr

✗

setAttribute

void

setAttributeNode

Attr

setAttributeNodeNS

Attr

setAttributeNS

Attr

✗

setIdAttribute

void

✗

✗

setIdAttributeNode

void

✗

✗

setIdAttributeNS

void

✗

✗

Attr

Since XML attributes may contain text values or entity references, DOM stores attributes in their own tree structure. This XML fragment contains one element with two attributes:

<!ENTITY bookcase_pic SYSTEM "bookcase.gif" NDATA gif>
<!ELEMENT picture EMPTY>
<!ATTLIST picture
   src ENTITY #REQUIRED
   alt CDATA #IMPLIED>
. . .
<picture src="bookcase_pic" alt="3/4 view of bookcase"/>

The first contains an unparsed entity reference, the second a simple string. Since DOM stores attributes as instances of Attr, some parsers represent their contents as subtrees of Node objects. Here, the attribute src would contain an instance of EntityReference. Note that the nodeValue of the Attr node contains the text representation of its children, the children of the Attr node. The following table lists the attributes and methods of the Attr.

Table: The Attr interface derived from Node

Name

Type

Read-only?

2.0

3.0

Attribute

specified

boolean

✗

isId

boolean

✗

✗

name

DOMString

✗

value

DOMString

ownerElement

Element

✗

✗

schemaTypeInfo

TypeInfo

✗

✗

Besides the attribute name and text value, the interface Attr contains a flag named specified showing whether the value was explicit in the XML or implicit through a !ATTLIST-declaration in the DTD. It also has a back-reference to the Element node containing the Attr instance.

CharacterData

Some data types in a DOM tree represent blocks of character data without markup. CharacterData is an abstract interface supporting common text-manipulation methods. The concrete interfaces Comment, Text and CDATASection use these methods. The following table lists attributes and methods supported by the CharacterData interface.

Table: The CharacterData interface derived from Node

Name

Type

Read-only?

DOM 2.0

Attribute

data

DOMString

length

unsigned long

✗

Methoden

appendData

void

deleteData

void

insertData

void

replaceData

void

Comment

DOM parsers need not expose XML comments after parsing. Depending on comment data is therefore poor programming practice at best. For metadata outside the underlying XML document’s actual content, consider processing instructions instead. The interface Comment derives from CharacterData and has only inherited attributes and methods, none specific to the interface.

EntityReference

A DOM parser may expose general entity references within XML elements as EntityReference nodes. This is not guaranteed: the parser can instead replace entities or character references with the Unicode strings they represent. The interface EntityReference derives from Node and has no specific attributes or methods.

Text

Character data from XML is stored in Text nodes. Text nodes are children of Element or Attr nodes. Immediately after parsing, every contiguous block of character data in the source becomes one Text node. The receiving application can still insert, delete or split Text nodes after parsing, allowing Text nodes to appear side by side in the tree. The following table describes the Text interface.

Table: The Text interface derived from CharacterData

Name

Type

Read-only?

2.0

3.0

Attribute

isElementContentWhitespace

boolean

✗

wholeText

DOMString

✗

✗

Methoden

replaceWholeText

Text

✗

splitText

Text

The method splitText splits an existing Text node into two at a specified position. This helps when inserting markup into existing character data: new nodes can be inserted into the resulting gap.

Level 3 also added the attribute wholeText, which returns all text, in document order, from the selected Text node and all neighboring Text nodes. Before Level 3, applications had to enumerate all children and concatenate them manually to obtain a node’s full text.

CDATASection

CDATA sections conveniently embed characters normally interpreted as markup in XML. The DOM hierarchy represents them as CDATASection nodes. The interface CDATASection derives from Text and has no specific attributes or methods.

  

 

 

 

back to top