XXVI. DOM Functions

Introduction

The DOM extension is the replacement for the DOM XML extension from PHP 4. The extension still contains many old functions, but they should no longer be used. In particular, functions that are not object-oriented should be avoided.

The extension allows you to operate on an XML document with the DOM API.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

Table 1. XML constants

ConstantValueDescription
XML_ELEMENT_NODE (integer) 1Node is an element
XML_ATTRIBUTE_NODE (integer) 2Node is an attribute
XML_TEXT_NODE (integer) 3Node is a piece of text
XML_CDATA_SECTION_NODE (integer) 4 
XML_ENTITY_REF_NODE (integer) 5 
XML_ENTITY_NODE (integer) 6Node is an entity like  
XML_PI_NODE (integer) 7Node is a processing instruction
XML_COMMENT_NODE (integer) 8Node is a comment
XML_DOCUMENT_NODE (integer) 9Node is a document
XML_DOCUMENT_TYPE_NODE (integer) 10 
XML_DOCUMENT_FRAG_NODE (integer) 11 
XML_NOTATION_NODE (integer) 12 
XML_HTML_DOCUMENT_NODE (integer) 13 
XML_DTD_NODE (integer) 14 
XML_ELEMENT_DECL_NODE (integer) 15 
XML_ATTRIBUTE_DECL_NODE (integer) 16 
XML_ENTITY_DECL_NODE (integer) 17 
XML_NAMESPACE_DECL_NODE (integer) 18 
XML_ATTRIBUTE_CDATA (integer) 1 
XML_ATTRIBUTE_ID (integer) 2 
XML_ATTRIBUTE_IDREF (integer) 3 
XML_ATTRIBUTE_IDREFS (integer) 4 
XML_ATTRIBUTE_ENTITY (integer) 5 
XML_ATTRIBUTE_NMTOKEN (integer) 7 
XML_ATTRIBUTE_NMTOKENS (integer) 8 
XML_ATTRIBUTE_ENUMERATION (integer) 9 
XML_ATTRIBUTE_NOTATION (integer) 10 

Table 2. DOMException constants

ConstantValueDescription
DOM_INDEX_SIZE_ERR (integer) 1 
DOMSTRING_SIZE_ERR (integer) 2 
DOM_HIERARCHY_REQUEST_ERR (integer) 3 
DOM_WRONG_DOCUMENT_ERR (integer) 4 
DOM_INVALID_CHARACTER_ERR (integer) 5 
DOM_NO_DATA_ALLOWED_ERR (integer) 6 
DOM_NO_MODIFICATION_ALLOWED_ERR (integer) 7 
DOM_NOT_FOUND_ERR (integer) 8 
DOM_NOT_SUPPORTED_ERR (integer) 9 
DOM_INUSE_ATTRIBUTE_ERR (integer) 10 
DOM_INVALID_STATE_ERR (integer) 11 
DOM_SYNTAX_ERR (integer) 12 
DOM_INVALID_MODIFICATION_ERR (integer) 13 
DOM_NAMESPACE_ERR (integer) 14 
DOM_INVALID_ACCESS_ERR (integer) 15 
DOM_VALIDATION_ERR (integer) 16 

Classes

The API of the module follows the DOM Level 2 standard as closely as possible. Consequently, the API is fully object-oriented. It is a good idea to have the DOM standard available when using this module.

This module defines a number of classes, which are listed - including their method - in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.

Table 3. List of classes

Class nameParent classes
DOMAttrDOMNode
DOMCDataSectionDOMText
DOMCharacterDataDOMNode
DOMCommentDOMCharacterData
DOMDocumentDOMNode
DOMDocumentFragmentDOMNode
DOMDocumentTypeDOMNode
DOMElementDOMNode
DOMEntityDOMNode
DOMEntityReferenceDOMNode
DOMNode 
DOMNotationDOMNode
DOMProcessingInstructionDOMNode
DOMTextDOMCharacterData
DOMException 
DOMImplementation 
DOMNamedNodeMap 
DOMNodeList 
DOMXPath 

Table of Contents
DOMAttr->isId --  Checks if attribute is a defined ID
DOMCharacterData->appendData --  Append the string to the end of the character data of the node
DOMCharacterData->deleteData --  Remove a range of characters from the node
DOMCharacterData->insertData --  Insert a string at the specified 16-bit unit offset
DOMCharacterData->replaceData --  Replace a substring within the DOMCharacterData node
DOMCharacterData->substringData --  Extracts a range of data from the node
DOMDocument->createAttribute -- Create new attribute
DOMDocument->createAttributeNS --  Create new attribute node with an associated namespace
DOMDocument->createCDATASection -- Create new cdata node
DOMDocument->createComment -- Create new comment node
DOMDocument->createDocumentFragment -- Create new document fragment
DOMDocument->createElement -- Create new element node
DOMDocument->createElementNS --  Create new element node with an associated namespace
DOMDocument->createEntityReference -- Create new entity reference node
DOMDocument->createProcessingInstruction -- Creates new PI node
DOMDocument->createTextNode -- Create new text node
DOMDocument->getElementById -- Searches for an element with a certain id
DOMDocument->getElementsByTagName -- Searches for all elements with given tag name
DOMDocument->getElementsByTagNameNS --  Searches for all elements with given tag name in specified namespace
DOMDocument->importNode -- Import node into current document
DOMDocument->load --  Load XML from a file
DOMDocument->loadHTML --  Load HTML from a string
DOMDocument->loadHTMLFile --  Load HTML from a file
DOMDocument->loadXML --  Load XML from a string
DOMDocument->normalize --  Normalizes document
DOMDocument->relaxNGValidate --  Performs relaxNG validation on the document
DOMDocument->relaxNGValidateSource --  Performs relaxNG validation on the document
DOMDocument->save --  Dumps the internal XML tree back into a file
DOMDocument->saveHTML --  Dumps the internal document into a string using HTML formatting
DOMDocument->saveHTMLFile --  Dumps the internal document back into a file using HTML formatting
DOMDocument->saveXML -- Dumps the internal XML tree back into a string
DOMDocument->schemaValidate --  Validates a document based on a schema
DOMDocument->schemaValidateSource --  Validates a document based on a schema
DOMDocument->validate --  Validates the document based on its DTD
DOMDocument->xinclude --  Substitutes XIncludes in a DOMDocument Object
DOMElement->getAttribute -- Returns value of attribute
DOMElement->getAttributeNode -- Returns attribute node
DOMElement->getAttributeNodeNS --  Returns attribute node
DOMElement->getAttributeNS -- Returns value of attribute
DOMElement->getElementsByTagName -- Gets elements by tagname
DOMElement->getElementsByTagNameNS -- Get elements by namespaceURI and localName
DOMElement->hasAttribute -- Checks to see if attribute exists
DOMElement->hasAttributeNS --  Checks to see if attribute exists
DOMElement->removeAttribute -- Removes attribute
DOMElement->removeAttributeNode -- Removes attribute
DOMElement->removeAttributeNS -- Removes attribute
DOMElement->setAttribute -- Adds new attribute
DOMElement->setAttributeNode -- Adds new attribute node to element
DOMElement->setAttributeNodeNS -- Adds new attribute node to element
DOMElement->setAttributeNS -- Adds new attribute
DOMImplementation->createDocument --  Creates a DOM Document object of the specified type with its document element
DOMImplementation->createDocumentType --  Creates an empty DOMDocumentType object
DOMImplementation->hasFeature --  Test if the DOM implementation implements a specific feature and version
DOMNamedNodeMap->getNamedItem --  Retrieves a node specified by name
DOMNamedNodeMap->getNamedItemNS --  Retrieves a node specified by local name and namespace URI
DOMNamedNodeMap->item -- Retrieves a node specified by index
DOMNode->appendChild --  Adds new child at the end of the children
DOMNode->cloneNode --  Clones a node
DOMNode->hasAttributes --  Checks if node has attributes
DOMNode->hasChildNodes --  Checks if node has children
DOMNode->insertBefore --  Adds new child at the end of the children
DOMNode->isSameNode --  Indicates if two nodes are the same node
DOMNode->isSupported --  Checks if feature is supported for specified version
DOMNode->lookupNamespaceURI --  Returns namespace URI of the node based on the prefix
DOMNode->lookupPrefix --  Returns name space prefix of the node based on namespaceURI
DOMNode->normalize --  Normalizes the node
DOMNode->removeChild --  Removes child from list of children
DOMNode->replaceChild --  Replaces a child
DOMNodelist->item --  Retrieves a node specified by index
DOMText->isWhitespaceInElementContent --  Indicates whether this text node contains whitespace
DOMText->splitText --  Breaks this node into two nodes at the specified offset
DOMXPath->query --  Evaluates the XPath expression in the given string
DOMXPath->registerNamespace --  Registers the namespace with the DOMXpath object
dom_import_simplexml --  Get a DOMElement object from a SimpleXMLElement object