XML technologies / XML / Perl & XML / DOM / Class API reference for DOM

(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)

DOM is increasingly becoming the preferred interface in the Perl XML world, making a detailed examination worthwhile. The following sections each describe a class, its purpose, properties and methods.

Warning! The DOM specification recommends UTF-16 as the standard encoding system. Most Perl implementations, however, use UTF-8. Current Perl versions make characters longer than 8 bits rather difficult to handle. Future versions should improve this, making UTF-16 straightforward to use.

Document

The class Document controls the document as a whole. It can create new objects and provides access to abstract information such as the document type declaration or the root element.

Properties

doctype
The document type declaration (DTD).

documentElement
The document’s root element.

Methods

createElement, createTextNode, createComment, createCDATASection, createProcessingInstruction, createAttribute, createEntityReference
Each method creates a new object of the corresponding type.

createElementNS, createAttributeNS (DOM2 only)
Like createElement and createAttribute, but taking an additional argument besides the element or attribute name: the namespace.

createDocumentFragment
Creates a container object for a document subtree.

getElementsByTagName
Returns a NodeList containing all child elements of a node with the specified element name.

getElementsByTagNameNS (DOM2 only)
Like getElementsByTagName, but also takes a namespace argument besides the element name. The asterisk (*) is a wildcard matching any element name or namespace.

getElementById (DOM2 only)
Finds the element with the specified ID attribute value.

importNode (DOM2 only)
Copies a node, which may come from another document. Think of it as importing markup by copying from the clipboard.

DocumentFragment

The class DocumentFragment holds a document fragment. It has a list of any number of children, each containing a document subtree. Unlike Document, it may have more than one Element child. A DocumentFragment need not follow well-formedness rules in this respect, but must follow them in all others, such as special characters in text.

It has no specific properties or methods, but inherits Node’s generic methods for accessing children.

DocumentType

This class accesses information in the document type declaration at the beginning of the document, but only its internal subset.

No specific methods are defined, but several properties are; all are read-only.

Properties

name
The root element’s name.

entities
A NamedNodeMap whose elements are entity declarations.

notation
A NamedNodeMap whose elements are notation declarations.

internalSubset (DOM2 only)
The DTD internal subset as a string.

publicId (DOM2 only)
The public identifier of the DTD external subset.

systemId (DOM2 only)
The system identifier of the DTD external subset.

Node

All other classes derive from Node. Its properties and methods are available on all DOM objects. Some properties are undefined for certain node types, such as an element’s value. Generic nodes help when writing code for several node types. For specific node types, the methods of the corresponding class are usually more convenient and simpler.

All properties except nodeValue and prefix are read-only.

Properties

node Name
Defined for elements, attributes and entities. For elements, it contains the element name.

nodeValue
Defined for attributes, text nodes, CDATA sections, processing instructions and comments.

nodeType
Contains one of these node types: Element, Attr, Text, CDATASection, EntityReference, Entity, ProcessingInstruction, Comment, Document, DocumentType, DocumentFragment or Notation.

parentNode
A reference to the parent node.

childNodes
An ordered list of references to this node’s children, if any.

firstChild, lastChild
References to the first and last child, if any.

previousSibling, nextSibling
This node’s preceding or following sibling.

attributes
An unordered list (NamedNodeMap) of nodes representing its attributes, if any.

ownerDocument
A reference to the document node, particularly useful for creating new nodes.

namespaceURI (DOM2 only)
If this node has a namespace prefix, this property contains the corresponding namespace URI; otherwise, null.

prefix (DOM2 only)
A namespace prefix, if any.

Methods

insertBefore
Inserts a new child before a specified child.

replaceChild
Replaces an existing child with a new one and returns the old child.

appendChild
Appends a new node to the child list.

hasChildNodes
Returns true if this node has children, otherwise false.

cloneNode
Copies this node, providing another way to obtain new nodes. The new node has the same properties, except parentNode, previousSibling and so on, which are all undefined. Cloned elements retain their attributes. The method takes an argument named deep. If true, children and all their descendants are also copied; otherwise, the property childNodes holds an empty list.

hasAttributes (DOM2 only)
Returns true if this node has attributes.

isSupported (DOM2 only)
Returns true if this DOM implementation supports a specified feature.

NodeList

Contains an ordered node list. The list is live: modifying it immediately changes the document accordingly.

Properties

length The number of nodes in the list.

Methods

item
Returns a reference to node number n in the list. The number n is the method’s argument; zero denotes the first node.

NamedNodeMap

An unordered node set allowing access by name. Nodes can also be addressed by index for enumeration, but the index implies no dependable ordering.

Properties

length
The number of nodes in the list.

Methods

getNamedItem, setNamedItem
Gets or sets a node whose name, if needed, is read from the nodeName property.

removeNamedItem
Removes and returns the node with the specified name.

item
Returns node number n in the list. This method merely permits iteration and implies no ordering.

getNamedItemNS (DOM2 only)
Returns the node with the specified fully qualified name, combining namespace and local name.

removeNamedItemNS (DOM2 only)
Removes the element with the specified fully qualified name from the list.

setNamedItemNS (DOM2 only)
Sets the node with the specified fully qualified name.

CharacterData

This class extends Node to access contained character data. Its subclasses are Text, CDATASection, Comment and ProcessingInstruction.

Properties

data
The contained character data.

length
The number of characters in the property data.

Methods

appendData
Appends a string to the data property.

substringData
Extracts a substring from the property data containing characters from offset through offset + count.

insertData
Inserts a string into the data property at position offset.

deleteData
Resets the property data to the empty string.

replaceData
Replaces a substring of the property data.

Element

This is the most important node type and the one you will usually work with. An element can have other nodes as children, particularly attribute nodes.

Properties

tagname
The element’s name.

Methods

getAttribute, getAttributeNode
Returns the attribute value or attribute node with the specified name.

setAttribute, setAttributeNode
Sets the attribute value or attribute node with the specified name.

removeAttribute, removeAttributeNode
Removes the attribute with the specified name, or the specified attribute node, from the element. Returns the removed node.

getElementsByTagName
Returns a NodeList of all contained elements with the specified name.

normalize
A child list can contain consecutive text nodes. This method merges them into one text node, useful when comparing nodes, for example. 

getAttributeNS (DOM2 only)
Returns the attribute node’s value for the specified fully qualified name: namespace URI plus local name.

getAttributeNodeNS (DOM2 only)
Like getAttributeNS, but returns the attribute node rather than its value.

getElementsByTagNamesNS (DOM2 only)
Like getElementsByTagName, but takes a fully qualified name as its argument.

hasAttribute (DOM2 only)
Returns true if the element has an attribute with the specified name.

hasAttributeNS (DOM2 only)
Like hasAttribute, but with a fully qualified name.

removeAttributeNS (DOM2 only)
Removes the attribute with the specified fully qualified name from the element.

setAttributeNS (DOM2 only)
Sets the attribute with the specified fully qualified name to the supplied value.

setAttributeNodeNS (DOM2 only)
Like setAttributeNS, but takes the new attribute node instead of the new value.

Attr

Properties

name
The attribute name.

specified
True if the attribute was explicitly set in the input document or by the program. False if it was supplied by a DTD default.

value
The attribute value as a text node.

ownerElement (DOM2 only)
The element node owning this attribute.

Text

Methods

splitText
Splits the text node into consecutive nodes, each containing part of the original text. The first ends before character number offset, the supplied argument. The second contains the remainder. Useful for inserting more nodes between the resulting nodes.

CDATASection

CDATASection is like a text node, but permits special characters such as < and & without escaping them as entity references. Its character data is accessible through the generic methods of the Node class.

ProcessingInstruction

Properties

target
The target of the processing instruction.

data
The processing instruction’s data.

Comment

Represents a comment. Its character data is accessible through the generic methods of the Node class.

EntityReference

A reference to a node of class Entity. Parsers usually resolve entity references and replace them with their values. With a different configuration, a parser produces these nodes instead. No methods explicitly force resolution, though it can occur as a side effect of other actions.

Entity

This class accesses an entity declared in the document’s internal DTD subset.

Properties

publicId
A resource’s public identifier, for an external entity.

systemId
A resource’s system identifier, for an external entity.

notationName
For an entity the parser cannot resolve, this contains a reference to a notation.

Notation

An object of class Notation represents a notation declaration in the internal DTD subset.

Properties

publicId
The notation’s public identifier.

systemId
The notation’s system identifier.

  

  

 

 

 

back to top