DOM XML
DomAttribute->name
DOM XML
PHP Manual

# DOM XML Functions

## Deprecated functions

There are quite a few functions that do not fit into the DOM standard and should no longer be used. These functions are listed in the following table. The function DomNode_append_child() has changed its behaviour. It now adds a child and not a sibling. If this breaks your application, use the non-DOM function DomNode_append_sibling().

Deprecated functions and their replacements
Old function | New function
--- | ---
xmldoc | domxml_open_mem()
xmldocfile | domxml_open_file()
domxml_new_xmldoc | domxml_new_doc()
domxml_dump_mem | DomDocument_dump_mem()
domxml_dump_mem_file | DomDocument_dump_file()
DomDocument_dump_mem_file | DomDocument_dump_file()
DomDocument_add_root | DomDocument_create_element() followed by DomNode_append_child()
DomDocument_dtd | DomDocument_doctype()
DomDocument_root | DomDocument_document_element()
DomDocument_children | DomNode_child_nodes()
DomDocument_imported_node | No replacement.
DomNode_add_child | Create a new node with e.g. DomDocument_create_element() and add it with DomNode_append_child().
DomNode_children | DomNode_child_nodes()
DomNode_parent | DomNode_parent_node()
DomNode_new_child | Create a new node with e.g. DomDocument_create_element() and add it with DomNode_append_child().
DomNode_set_content | Create a new node with e.g. DomDocument_create_text_node() and add it with DomNode_append_child().
DomNode_get_content | Content is just a text node and can be accessed with DomNode_child_nodes().
DomNode_set_content | Content is just a text node and can be added with DomNode_append_child().

## 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. Though the API is object-oriented, there are many functions which can be called in a non-object-oriented way by passing the object to operate on as the first argument. These functions are mainly to retain compatibility to older versions of the extension, and should not be used when creating new scripts.

This API differs from the official DOM API in two ways. First, all class attributes are implemented as functions with the same name. Secondly, the function names follow the PHP naming convention. This means that a DOM function lastChild() will be written as last_child().

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.

List of classes
Class name | Parent classes
--- | ---
DomAttribute | DomNode
DomCData | DomNode
DomComment | DomCData : DomNode
DomDocument | DomNode
DomDocumentType | DomNode
DomElement | DomNode
DomEntity | DomNode
DomEntityReference | DomNode
DomProcessingInstruction | DomNode
DomText | DomCData : DomNode
Parser | Currently still called DomParser
XPathContext |

DomDocument class (DomDocument : DomNode)
Method name | Function name | Remark
--- | --- | ---
doctype | DomDocument_doctype() |
document_element | DomDocument_document_element() |
create_element | DomDocument_create_element() |
create_text_node | DomDocument_create_text_node() |
create_comment | DomDocument_create_comment() |
create_cdata_section | DomDocument_create_cdata_section() |
create_processing_instruction | DomDocument_create_processing_instruction() |
create_attribute | DomDocument_create_attribute() |
create_entity_reference | DomDocument_create_entity_reference() |
get_elements_by_tagname | DomDocument_get_elements_by_tagname() |
get_element_by_id | DomDocument_get_element_by_id() |
dump_mem | DomDocument_dump_mem() | not DOM standard
dump_file | DomDocument_dump_file() | not DOM standard
html_dump_mem | DomDocument_html_dump_mem() | not DOM standard
xpath_init | xpath_init | not DOM standard
xpath_new_context | xpath_new_context | not DOM standard
xptr_new_context | xptr_new_context | not DOM standard

DomElement class (DomElement : DomNode)
Method name | Function name | Remark
--- | --- | ---
tagname | DomElement_tagname() |
get_attribute | DomElement_get_attribute() |
set_attribute | DomElement_set_attribute() |
remove_attribute | DomElement_remove_attribute() |
get_attribute_node | DomElement_get_attribute_node() |
set_attribute_node | DomElement_set_attribute_node() |
get_elements_by_tagname | DomElement_get_elements_by_tagname() |
has_attribute | DomElement_has_attribute() |

DomNode class
Method name | Remark
--- | ---
DomNode_node_name() |
DomNode_node_value() |
DomNode_node_type() |
DomNode_last_child() |
DomNode_first_child() |
DomNode_child_nodes() |
DomNode_previous_sibling() |
DomNode_next_sibling() |
DomNode_parent_node() |
DomNode_owner_document() |
DomNode_insert_before() |
DomNode_append_child() |
DomNode_append_sibling() | Not in DOM standard. This function emulates the former behaviour of DomNode_append_child().
DomNode_remove_child() |
DomNode_has_child_nodes() |
DomNode_has_attributes() |
DomNode_clone_node() |
DomNode_attributes() |
DomNode_unlink_node() | Not in DOM standard
DomNode_replace_node() | Not in DOM standard
DomNode_set_content() | Not in DOM standard, deprecated
DomNode_get_content() | Not in DOM standard, deprecated
DomNode_dump_node() | Not in DOM standard
DomNode_is_blank_node() | Not in DOM standard

DomAttribute class (DomAttribute : DomNode)
Method name |  | Remark
--- | --- | ---
name | DomAttribute_name() |
value | DomAttribute_value() |
specified | DomAttribute_specified() |

DomProcessingInstruction class (DomProcessingInstruction : DomNode)
Method name | Function name | Remark
--- | --- | ---
target | DomProcessingInstruction_target() |
data | DomProcessingInstruction_data() |

Parser class
Method name | Function name | Remark
--- | --- | ---
add_chunk | Parser_add_chunk() |
end | Parser_end() |

XPathContext class
Method name | Function name | Remark
--- | --- | ---
eval | XPathContext_eval() |
eval_expression | XPathContext_eval_expression() |
register_ns | XPathContext_register_ns() |

DomDocumentType class (DomDocumentType : DomNode)
Method name | Function name | Remark
--- | --- | ---
name | DomDocumentType_name() |
entities | DomDocumentType_entities() |
notations | DomDocumentType_notations() |
public_id | DomDocumentType_public_id() |
system_id | DomDocumentType_system_id() |
internal_subset | DomDocumentType_internal_subset() |

The classes DomDtd is derived from DomNode. DomComment is derived from DomCData.

## Examples

Many examples in this reference require an XML string. Instead of repeating this string in every example, it will be put into a file which will be included by each example. This include file is shown in the following example section. Alternatively, you could create an XML document and read it with DomDocument_open_file().

Example #1 Include file example.inc with XML string

`<?php
$xmlstr = "<?xml version='1.0' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<!-- lsfj -->
<chapter language='en'><title language='en'>Title</title>
<para language='ge'>
&sp;
<!-- comment -->
<informaltable ID='findme' language='&sp;'>
<tgroup cols='3'>
<tbody>
<row><entry>a1</entry><entry
morerows='1'>b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
</tbody>
</tgroup>
</informaltable>
</para>
</chapter>";
?>`

## Table of Contents

- DomAttribute->name — Returns the name of attribute
- DomAttribute->set_value — Sets the value of an attribute
- DomAttribute->specified — Checks if attribute is specified
- DomAttribute->value — Returns value of attribute
- DomDocument->add_root — Adds a root node [deprecated]
- DomDocument->create_attribute — Create new attribute
- DomDocument->create_cdata_section — Create new cdata node
- DomDocument->create_comment — Create new comment node
- DomDocument->create_element_ns — Create new element node with an associated namespace
- DomDocument->create_element — Create new element node
- DomDocument->create_entity_reference — Create an entity reference
- DomDocument->create_processing_instruction — Creates new PI node
- DomDocument->create_text_node — Create new text node
- DomDocument->doctype — Returns the document type
- DomDocument->document_element — Returns root element node
- DomDocument->dump_file — Dumps the internal XML tree back into a file
- DomDocument->dump_mem — Dumps the internal XML tree back into a string
- DomDocument->get_element_by_id — Searches for an element with a certain id
- DomDocument->get_elements_by_tagname — Returns array with nodes with given tagname in document or empty array, if not found
- DomDocument->html_dump_mem — Dumps the internal XML tree back into a string as HTML
- DomDocument->xinclude — Substitutes XIncludes in a DomDocument Object
- DomDocumentType->entities — Returns list of entities
- DomDocumentType->internal_subset — Returns internal subset
- DomDocumentType->name — Returns name of document type
- DomDocumentType->notations — Returns list of notations
- DomDocumentType->public_id — Returns public id of document type
- DomDocumentType->system_id — Returns the system id of document type
- DomElement->get_attribute_node — Returns the node of the given attribute
- DomElement->get_attribute — Returns the value of the given attribute
- DomElement->get_elements_by_tagname — Gets elements by tagname
- DomElement->has_attribute — Checks to see if an attribute exists in the current node
- DomElement->remove_attribute — Removes attribute
- DomElement->set_attribute_node — Adds new attribute
- DomElement->set_attribute — Sets the value of an attribute
- DomElement->tagname — Returns the name of the current element
- DomNode->add_namespace — Adds a namespace declaration to a node
- DomNode->append_child — Adds a new child at the end of the children
- DomNode->append_sibling — Adds new sibling to a node
- DomNode->attributes — Returns list of attributes
- DomNode->child_nodes — Returns children of node
- DomNode->clone_node — Clones a node
- DomNode->dump_node — Dumps a single node
- DomNode->first_child — Returns first child of node
- DomNode->get_content — Gets content of node
- DomNode->has_attributes — Checks if node has attributes
- DomNode->has_child_nodes — Checks if node has children
- DomNode->insert_before — Inserts new node as child
- DomNode->is_blank_node — Checks if node is blank
- DomNode->last_child — Returns last child of node
- DomNode->next_sibling — Returns the next sibling of node
- DomNode->node_name — Returns name of node
- DomNode->node_type — Returns type of node
- DomNode->node_value — Returns value of a node
- DomNode->owner_document — Returns the document this node belongs to
- DomNode->parent_node — Returns the parent of the node
- DomNode->prefix — Returns name space prefix of node
- DomNode->previous_sibling — Returns the previous sibling of node
- DomNode->remove_child — Removes child from list of children
- DomNode->replace_child — Replaces a child
- DomNode->replace_node — Replaces node
- DomNode->set_content — Sets content of node
- DomNode->set_name — Sets name of node
- DomNode->set_namespace — Sets namespace of a node
- DomNode->unlink_node — Deletes node
- DomProcessingInstruction->data — Returns the data of ProcessingInstruction node
- DomProcessingInstruction->target — Returns the target of a ProcessingInstruction node
- DomXsltStylesheet->process — Applies the XSLT-Transformation on a DomDocument Object
- DomXsltStylesheet->result_dump_file — Dumps the result from a XSLT-Transformation into a file
- DomXsltStylesheet->result_dump_mem — Dumps the result from a XSLT-Transformation back into a string
- domxml_new_doc — Creates new empty XML document
- domxml_open_file — Creates a DOM object from an XML file
- domxml_open_mem — Creates a DOM object of an XML document
- domxml_version — Gets the XML library version
- domxml_xmltree — Creates a tree of PHP objects from an XML document
- domxml_xslt_stylesheet_doc — Creates a DomXsltStylesheet Object from a DomDocument Object
- domxml_xslt_stylesheet_file — Creates a DomXsltStylesheet Object from an XSL document in a file
- domxml_xslt_stylesheet — Creates a DomXsltStylesheet object from an XSL document in a string
- domxml_xslt_version — Gets the XSLT library version
- xpath_eval_expression — Evaluates the XPath Location Path in the given string
- xpath_eval — Evaluates the XPath Location Path in the given string
- xpath_new_context — Creates new xpath context
- xpath_register_ns_auto — Register the given namespace in the passed XPath context
- xpath_register_ns — Register the given namespace in the passed XPath context
- xptr_eval — Evaluate the XPtr Location Path in the given string
- xptr_new_context — Create new XPath Context

DOM XML
DomAttribute->name
DOM XML
PHP Manual