DOM Functions DOM
&reftitle.intro; The DOM extension allows you to operate on XML documents through the DOM API with PHP 5. For PHP 4, use DOM XML. DOM extension uses UTF-8 encoding. Use utf8_encode and utf8_decode to work with texts in ISO-8859-1 encoding or Iconv for other encodings.
&reftitle.install; &no.install;
&reftitle.classes; The API of the module follows the DOM Level 3 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 explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.
<classname>DOMAttr</classname> Extends DOMNode. The DOMAttr interface represents an attribute in an DOMElement object.
&reftitle.constructor; - construct a new DOMAttr object
&reftitle.methods; - Checks if attribute is a defined ID
&reftitle.properties; <tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>name</entry> <entry>string</entry> <entry>yes</entry> <entry>The name of the attribute</entry> </row> <row> <entry>ownerElement</entry> <entry>DOMElement</entry> <entry>yes</entry> <entry>The element which contains the attribute</entry> </row> <row> <entry>schemaTypeInfo</entry> <entry>bool</entry> <entry>yes</entry> <entry>Not implemented yet, always return &null;</entry> </row> <row> <entry>specified</entry> <entry>bool</entry> <entry>yes</entry> <entry>Not implemented yet, always return &null;</entry> </row> <row> <entry>value</entry> <entry>string</entry> <entry>no</entry> <entry>The value of the attribute</entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domcharacterdata'> <title><classname>DOMCharacterData</classname> Extends DOMNode.
&reftitle.methods; - Append a string to the end of the character data of the node - Remove a range of characters from the node - Insert a string at the specified 16-bit unit offset - Replace a substring within the DOMCharacterData node - Extracts a range of data from the node
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>data</entry> <entry>string</entry> <entry>no</entry> <entry>The contents of the node</entry> </row> <row> <entry>length</entry> <entry>int</entry> <entry>yes</entry> <entry>The length of the contents</entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domcomment'> <title><classname>DOMComment</classname> Extends DOMCharacterData.
&reftitle.constructor; - construct a new DOMComment object
<classname>DOMDocument</classname> Extends DOMNode.
&reftitle.constructor; - construct a new DOMDocument object
&reftitle.methods; - Create new attribute - Create new attribute node with an associated namespace - Create new cdata node - Create new comment node - Create new document fragment - Create new element node - Create new element node with an associated namespace - Create new entity reference node - Creates new PI node - Create new text node - Searches for an element with a certain id - Searches for all elements with given tag name - Searches for all elements with given tag name in specified namespace - Import node into current document - Load XML from a file - Load HTML from a string - Load HTML from a file - Load XML from a string - Normalizes the document - Performs relaxNG validation on the document - Performs relaxNG validation on the document - Register extended class used to create base node type (not DOM standard) - Dumps the internal XML tree back into a file - Dumps the internal document into a string using HTML formatting - Dumps the internal document back into a file using HTML formatting - Dumps the internal XML tree back into a string - Validates a document based on a schema - Validates a document based on a schema - Validates the document based on its DTD - Substitutes XIncludes in a DOMDocument Object
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>actualEncoding</entry> <entry>string</entry> <entry>yes</entry> <entry></entry> </row> <row> <entry>config</entry> <entry>DOMConfiguration</entry> <entry>yes</entry> <entry></entry> </row> <row> <entry>doctype</entry> <entry>DOMDocumentType</entry> <entry>yes</entry> <entry> The Document Type Declaration associated with this document. </entry> </row> <row> <entry>documentElement</entry> <entry>DOMElement</entry> <entry>yes</entry> <entry> This is a convenience attribute that allows direct access to the child node that is the document element of the document. </entry> </row> <row> <entry>documentURI</entry> <entry>string</entry> <entry>no</entry> <entry> The location of the document or &null; if undefined. </entry> </row> <row> <entry>encoding</entry> <entry>string</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>formatOutput</entry> <entry>bool</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>implementation</entry> <entry>DOMImplementation</entry> <entry>yes</entry> <entry> The <classname>DOMImplementation</classname> object that handles this document. </entry> </row> <row> <entry>preserveWhiteSpace</entry> <entry>bool</entry> <entry>no</entry> <entry>Do not remove redundant white space. Default to &true;.</entry> </row> <row> <entry>recover</entry> <entry>bool</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>resolveExternals</entry> <entry>bool</entry> <entry>no</entry> <entry> Set it to &true; to load external entities from a doctype declaration. This is useful for including character entities in your XML document. </entry> </row> <row> <entry>standalone</entry> <entry>bool</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>strictErrorChecking</entry> <entry>bool</entry> <entry>no</entry> <entry>Throws DOMException on errors. Default to &true;.</entry> </row> <row> <entry>substituteEntities</entry> <entry>bool</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>validateOnParse</entry> <entry>bool</entry> <entry>no</entry> <entry>Loads and validates against the DTD. Default to &false;.</entry> </row> <row> <entry>version</entry> <entry>string</entry> <entry>no</entry> <entry></entry> </row> <row> <entry>xmlEncoding</entry> <entry>string</entry> <entry>yes</entry> <entry> An attribute specifying, as part of the XML declaration, the encoding of this document. This is &null; when unspecified or when it is not known, such as when the Document was created in memory. </entry> </row> <row> <entry>xmlStandalone</entry> <entry>bool</entry> <entry>no</entry> <entry> An attribute specifying, as part of the XML declaration, whether this document is standalone. This is &false; when unspecified. </entry> </row> <row> <entry>xmlVersion</entry> <entry>string</entry> <entry>no</entry> <entry> An attribute specifying, as part of the XML declaration, the version number of this document. If there is no declaration and if this document supports the "XML" feature, the value is "1.0". </entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domdocumentfragment'> <title><classname>DOMDocumentFragment</classname> Extends DOMNode.
&reftitle.methods; - Append raw XML data (not DOM standard)
<classname>DOMDocumentType</classname> Extends DOMNode Each DOMDocument has a doctype attribute whose value is either &null; or a DOMDocumentType object.
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>publicId</entry> <entry>string</entry> <entry>yes</entry> <entry>The public identifier of the external subset.</entry> </row> <row> <entry>systemId</entry> <entry>string</entry> <entry>yes</entry> <entry>The system identifier of the external subset. This may be an absolute URI or not.</entry> </row> <row> <entry>name</entry> <entry>string</entry> <entry>yes</entry> <entry>The name of DTD; i.e., the name immediately following the <literal>DOCTYPE</literal> keyword.</entry> </row> <row> <entry>entities</entry> <entry>DOMNamedNodeMap</entry> <entry>yes</entry> <entry> A <classname>DOMNamedNodeMap</classname> containing the general entities, both external and internal, declared in the DTD. </entry> </row> <row> <entry>notations</entry> <entry>DOMNamedNodeMap</entry> <entry>yes</entry> <entry> A <classname>DOMNamedNodeMap</classname> containing the notations declared in the DTD. </entry> </row> <row> <entry>internalSubset</entry> <entry>string</entry> <entry>yes</entry> <entry> The internal subset as a string, or null if there is none. This is does not contain the delimiting square brackets. </entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domelement'> <title><classname>DOMElement</classname> Extends DOMNode.
&reftitle.constructor; - construct a new DOMElement object
&reftitle.methods; - Returns value of attribute - Returns attribute node - Returns attribute node - Returns value of attribute - Gets elements by tagname - Get elements by namespaceURI and localName - Checks to see if attribute exists - Checks to see if attribute exists - Removes attribute - Removes attribute - Removes attribute - Adds new attribute - Adds new attribute node to element - Adds new attribute node to element - Adds new attribute - Declares the ID attribute - Declares the ID attribute - Declares the ID attribute
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>schemaTypeInfo</entry> <entry>bool</entry> <entry>yes</entry> <entry>Not implemented yet, always return &null;</entry> </row> <row> <entry>tagName</entry> <entry>string</entry> <entry>yes</entry> <entry>The element name</entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domentity'> <title><classname>DOMEntity</classname> Extends DOMNode This interface represents a known entity, either parsed or unparsed, in an XML document.
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>publicId</entry> <entry>string</entry> <entry>yes</entry> <entry> The public identifier associated with the entity if specified, and &null; otherwise. </entry> </row> <row> <entry>systemId</entry> <entry>string</entry> <entry>yes</entry> <entry> The system identifier associated with the entity if specified, and &null; otherwise. This may be an absolute URI or not. </entry> </row> <row> <entry>notationName</entry> <entry>string</entry> <entry>yes</entry> <entry> For unparsed entities, the name of the notation for the entity. For parsed entities, this is &null;. </entry> </row> <row> <entry>actualEncoding</entry> <entry>string</entry> <entry>no</entry> <entry> An attribute specifying the encoding used for this entity at the time of parsing, when it is an external parsed entity. This is &null; if it an entity from the internal subset or if it is not known. </entry> </row> <row> <entry>encoding</entry> <entry>string</entry> <entry>yes</entry> <entry> An attribute specifying, as part of the text declaration, the encoding of this entity, when it is an external parsed entity. This is &null; otherwise. </entry> </row> <row> <entry>version</entry> <entry>string</entry> <entry>yes</entry> <entry> An attribute specifying, as part of the text declaration, the version number of this entity, when it is an external parsed entity. This is &null; otherwise. </entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domentityreference'> <title><classname>DOMEntityReference</classname> Extends DOMNode.
&reftitle.constructor; - construct a new DOMEntityReference object
<classname>DOMException</classname> DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons. See also .
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>code</entry> <entry>int</entry> <entry>yes</entry> <entry>An integer indicating the type of error generated</entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domimplementation'> <title><classname>DOMImplementation</classname> The DOMImplementation interface provides a number of methods for performing operations that are independent of any particular instance of the document object model.
&reftitle.constructor; - construct a new DOMImplementation object
&reftitle.methods; - Creates a DOM Document object of the specified type with its document element - Creates an empty DOMDocumentType object - Test if the DOM implementation implements a specific feature
<classname>DOMNamedNodeMap</classname>
&reftitle.methods; - Retrieves a node specified by name - Retrieves a node specified by local name and namespace URI - Retrieves a node specified by index
<classname>DOMNode</classname>
&reftitle.methods; - Adds new child at the end of the children - Clones a node - Checks if node has attributes - Checks if node has children - Adds a new child before a reference node - Checks if the specified namespaceURI is the default namespace - Indicates if two nodes are the same node - Checks if feature is supported for specified version - Returns namespace URI of the node based on the prefix - Returns name space prefix of the node based on namespaceURI - Normalizes the node - Removes child from list of children - Replaces a child
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>nodeName</entry> <entry>string</entry> <entry>yes</entry> <entry>Returns the most accurate name for the current node type</entry> </row> <row> <entry>nodeValue</entry> <entry>string</entry> <entry>no</entry> <entry>The value of this node, depending on its type.</entry> </row> <row> <entry>nodeType</entry> <entry>int</entry> <entry>yes</entry> <entry>Gets the type of the node. One of the predefined XML_xxx_NODE constants</entry> </row> <row> <entry>parentNode</entry> <entry>DOMNode</entry> <entry>yes</entry> <entry>The parent of this node.</entry> </row> <row> <entry>childNodes</entry> <entry>DOMNodeList</entry> <entry>yes</entry> <entry> A <classname>DOMNodeList</classname> that contains all children of this node. If there are no children, this is an empty <classname>DOMNodeList</classname>. </entry> </row> <row> <entry>firstChild</entry> <entry>DOMNode</entry> <entry>yes</entry> <entry> The first child of this node. If there is no such node, this returns &null;. </entry> </row> <row> <entry>lastChild</entry> <entry>DOMNode</entry> <entry>yes</entry> <entry> The last child of this node. If there is no such node, this returns &null;. </entry> </row> <row> <entry>previousSibling</entry> <entry>DOMNode</entry> <entry>yes</entry> <entry> The node immediately preceding this node. If there is no such node, this returns &null;. </entry> </row> <row> <entry>nextSibling</entry> <entry>DOMNode</entry> <entry>yes</entry> <entry> The node immediately following this node. If there is no such node, this returns &null;. </entry> </row> <row> <entry>attributes</entry> <entry>DOMNamedNodeMap</entry> <entry>yes</entry> <entry> A <classname>DOMNamedNodeMap</classname> containing the attributes of this node (if it is a <classname>DOMElement</classname>) or &null; otherwise. </entry> </row> <row> <entry>ownerDocument</entry> <entry>DOMDocument</entry> <entry>yes</entry> <entry> The <classname>DOMDocument</classname> object associated with this node. </entry> </row> <row> <entry>namespaceURI</entry> <entry>string</entry> <entry>yes</entry> <entry> The namespace URI of this node, or &null; if it is unspecified. </entry> </row> <row> <entry>prefix</entry> <entry>string</entry> <entry>no</entry> <entry> The namespace prefix of this node, or &null; if it is unspecified. </entry> </row> <row> <entry>localName</entry> <entry>string</entry> <entry>yes</entry> <entry> Returns the local part of the qualified name of this node. </entry> </row> <row> <entry>baseURI</entry> <entry>string</entry> <entry>yes</entry> <entry> The absolute base URI of this node or &null; if the implementation wasn't able to obtain an absolute URI. </entry> </row> <row> <entry>textContent</entry> <entry>string</entry> <entry>no</entry> <entry> This attribute returns the text content of this node and its descendants. </entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domnodelist'> <title><classname>DOMNodeList</classname>
&reftitle.methods; - Retrieves a node specified by index
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>length</entry> <entry>int</entry> <entry>yes</entry> <entry> The number of nodes in the list. The range of valid child node indices is 0 to <literal>length - 1</literal> inclusive. </entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domnotation'> <title><classname>DOMNotation</classname> Extends DOMNode
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>publicId</entry> <entry>string</entry> <entry>yes</entry> <entry></entry> </row> <row> <entry>systemId</entry> <entry>string</entry> <entry>yes</entry> <entry></entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domprocessinginstruction'> <title><classname>DOMProcessingInstruction</classname> Extends DOMNode.
&reftitle.constructor; - construct a new DOMProcessingInstruction object
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>target</entry> <entry>string</entry> <entry>yes</entry> <entry></entry> </row> <row> <entry>data</entry> <entry>string</entry> <entry>no</entry> <entry></entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domtext'> <title><classname>DOMText</classname> Extends DOMCharacterData.
&reftitle.constructor; - construct a new DOMText object
&reftitle.methods; - Indicates whether this text node contains whitespace - Breaks the node into two nodes at the specified offset
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>wholeText</entry> <entry>string</entry> <entry>yes</entry> <entry></entry> </row> </tbody> </tgroup> </table> </section> </section> <section xml:id='dom.class.domxpath'> <title><classname>DOMXPath</classname>
&reftitle.constructor; - construct a new DOMXPath object
&reftitle.methods; - Registers the namespace with the DOMXpath object - Evaluates the given XPath expression and returns a typed result if possible - Evaluates the given XPath expression
&reftitle.properties;
<tgroup cols="4"> <thead> <row> <entry>Name</entry> <entry>Type</entry> <entry>Read-only</entry> <entry>Description</entry> </row> </thead> <tbody> <row> <entry>document</entry> <entry>DOMDocument</entry> <entry></entry> <entry></entry> </row> </tbody> </tgroup> </table> </section> </section> </section> <section xml:id="dom.examples"> &reftitle.examples; <para> Many examples in this reference require an XML file. We will use <filename>book.xml</filename> that contains the following: </para> <para> <example> <title>book.xml My lists My books Title Author Language ISBN The Grapes of Wrath John Steinbeck en 0140186409 The Pearl John Steinbeck en 014017737X Samarcande Amine Maalouf fr 2253051209 ]]> &reference.dom.constants; &reference.dom.functions;