DOM XML functionsDOM XML
&reftitle.intro;
&warn.experimental;
The DOM XML extension has been overhauled in PHP 4.3.0 to better comply with
the DOM standard. 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.
It also provides a function domxml_xmltree to turn the
complete XML document into a tree of PHP objects. Currently, this
tree should be considered read-only — you can modify it, but this
would not make any sense since DomDocument_dump_mem
cannot be
applied to it. Therefore, if you want to read an XML file and write
a modified version, use DomDocument_create_element,
DomDocument_create_text_node,
set_attribute, etc. and finally the
DomDocument_dump_mem function.
&reftitle.required;
This extension makes use of the
GNOME XML library. Download
and install this library. You will need at least libxml-2.4.14.
&reftitle.install;
This extension is only available if PHP was configured with
.
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 replacementsOld functionNew functionxmldocdomxml_open_memxmldocfiledomxml_open_filedomxml_new_xmldocdomxml_new_docdomxml_dump_memDomDocument_dump_memdomxml_dump_mem_fileDomDocument_dump_fileDomDocument_dump_mem_fileDomDocument_dump_fileDomDocument_add_rootDomDocument_create_element followed by
DomNode_append_childDomDocument_dtdDomDocument_doctypeDomDocument_rootDomDocument_document_elementDomDocument_childrenDomNode_child_nodesDomDocument_imported_nodeNo replacement.DomNode_add_childCreate a new node with e.g.
DomDocument_create_element und add it with
DomNode_append_child.
DomNode_childrenDomNode_child_nodesDomNode_parentDomNode_parent_nodeDomNode_new_childCreate a new node with e.g.
DomDocument_create_element and add it with
DomNode_append_child.
DomNode_set_contentCreate a new node with e.g.
DomDocument_create_text_node and add it with
DomNode_append_child.
DomNode_get_contentContent is just a text node and can be accessed with
DomNode_child_nodes.
DomNode_set_contentContent is just a text node and can be added with
DomNode_append_child.
&reference.domxml.constants;
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 compatibilty
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 classesClass nameParent classesDomAttributeDomNodeDomCDataDomNodeDomCommentDomCData : DomNodeDomDocumentDomNodeDomDocumentTypeDomNodeDomElementDomNodeDomEntityDomNodeDomEntityReferenceDomNodeDomProcessingInstructionDomNodeDomTextDomCData : DomNodeParserCurrently still called DomParserXPathContext
DomDocument class (DomDocument : DomNode)Method nameFunction nameRemarkdoctypeDomDocument_doctypedocument_elemnentDomDocument_document_elementcreate_elementDomDocument_create_elementcreate_text_nodeDomDocument_create_text_nodecreate_commentDomDocument_create_commentcreate_cdata_sectionDomDocument_create_cdata_sectioncreate_processing_instructionDomDocument_create_processing_instructioncreate_attributeDomDocument_create_attributecreate_entity_referenceDomDocument_create_entity_referenceget_elements_by_tagnameDomDocument_get_elements_by_tagnameget_element_by_idDomDocument_get_element_by_iddump_memDomDocument_dump_memnot DOM standarddump_fileDomDocument_dump_filenot DOM standardhtml_dump_memDomDocument_html_dump_memnot DOM standardxpath_initxpath_initnot DOM standardxpath_new_contextxpath_new_contextnot DOM standardxptr_new_contextxptr_new_contextnot DOM standard
DomElement class (DomElement : DomNode)Method nameFunction nameRemarktagnameDomElement_tagnameget_attributeDomElement_get_attributeset_attributeDomElement_set_attributeremove_attributeDomElement_remove_attributeget_attribute_nodeDomElement_get_attribute_nodeget_elements_by_tagnameDomElement_get_elements_by_tagnamehas_attributeDomElement_has_attribute
DomNode classMethod nameRemarkDomNode_node_nameDomNode_node_valueDomNode_node_typeDomNode_last_childDomNode_first_childDomNode_child_nodesDomNode_previous_siblingDomNode_next_siblingDomNode_parent_nodeDomNode_owner_documentDomNode_insert_beforeDomNode_append_childDomNode_append_siblingNot in DOM standard. This function emulates the former
behaviour of DomNode_append_child.DomNode_remove_childDomNode_has_child_nodesDomNode_has_attributesDomNode_clone_nodeDomNode_attributesDomNode_unlink_nodeNot in DOM standardDomNode_replace_nodeNot in DOM standardDomNode_set_contentNot in DOM standard, deprecatedDomNode_get_contentNot in DOM standard, deprecatedDomNode_dump_nodeNot in DOM standardDomNode_is_blank_nodeNot in DOM standard
DomAttribute class (DomAttribute : DomNode)Method nameRemarknameDomAttribute_namevalueDomAttribute_valuespecifiedDomAttribute_specified
DomProcessingInstruction class (DomProcessingInstruction : DomNode)Method nameFunction nameRemarktargetDomProcessingInstruction_targetdataDomProcessingInstruction_data
DomDocumentType class (DomDocumentType : DomNode)Method nameFunction nameRemarknameDomDocumentType_nameentitiesDomDocumentType_entitiesnotationsDomDocumentType_notationspublic_idDomDocumentType_public_idsystem_idDomDocumentType_system_idinternal_subsetDomDocumentType_internal_subset
The classes DomDtd is derived from DomNode. DomComment is derived from
DomCData.
&reftitle.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.
Include file example.inc with XML string
]>
Title
&sp;
a1b1c1a2c2a3b3c3";
?>
]]>
&reference.domxml.functions;