DOM XML functionsDOM XML
&warn.experimental;
This documentation is not finished yet. Don't start to translate it
or use it as a programming reference (steinm@php.net).
These functions are only available if PHP was configured with
, using the
GNOME xml library.
You will need at least libxml-2.2.7
These functions have been added in PHP 4.
The extension allows you to operate on an XML document with the DOM API.
It also provides a function 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 dumpmem cannot be
applied to it. Therefore, if you want to read an XML file and write
a modified version use the add_node,
set_attribute, etc. and finally
dumpmem functions.
This module defines the following constants:
XML constantsConstantValueDescriptionXML_ELEMENT_NODE1Node is an elementXML_ATTRIBUTE_NODE2Node is an attributeXML_TEXT_NODE3Node is a piece of textXML_CDATA_SECTION_NODE4XML_ENTITY_REF_NODE5XML_ENTITY_NODE6Node is an entity like XML_PI_NODE7Node is a processing instructionXML_COMMENT_NODE8Node is a commentXML_DOCUMENT_NODE9Node is a documentXML_DOCUMENT_TYPE_NODE10XML_DOCUMENT_FRAG_NODE11XML_NOTATION_NODE12XML_GLOBAL_NAMESPACE1XML_LOCAL_NAMESPACE2
Each function in this extension can be used in two ways. In a non-object
oriented way by passing the object to apply the function to as a first
argument, or in an object oriented way by calling the function as a method
of an object. This documentation describes the non-object oriented
functions, though you get the object methods by skipping the prefix
"domxml_".
This module defines a number of classes, which are listed —
including their
properties and method — in the following table.
DomDocument class (methods)Method nameFunction nameDescriptionrootdomxml_rootchildrendomxml_childrenadd_rootdomxml_add_rootdtddomxml_intdtddumpmemdomxmlxpath_initxpath_initxpath_new_contextxpath_new_contextxptr_new_contextxptr_new_context
DomDocument class (attributes)NameTypeDescriptiondocclass DomDocumentThe object itselfnamestringurlstringversionstringVersion of XMLencodingstringstandalonelong1 if the file is a standalone versiontypelongOne of the constants in table ... compressionlong1 if the file is compressedcharsetlong
DomNode class (methods)NamePHP nameDescriptionlastchilddomxml_last_childchildrendomxml_childrenparentdomxml_parentnew_childdomxml_new_childget_attributedomxml_get_attributeset_attributedomxml_set_attributeattributesdomxml_attributesnodedomxml_nodeset_contentdomxml_set_content
DomNode class (attributes)NameTypeDescriptionnodeclass DomNodeThe object itselftypelongnamestringcontentstring
xmldocCreates a DOM object of an XML documentDescriptionobject xmldocstring str
&warn.experimental.func;
The function parses the XML document in
str and returns an object of class "Dom
document", having the properties as listed above.
See also xmldocfilexmldocfileCreates a DOM object from XML fileDescriptionobject xmldocfilestring filename
&warn.experimental.func;
The function parses the XML document in the file named
filename and returns an object of class
"Dom document", having the properties as listed above.
The file is accessed read-only.
See also xmldocxmltree
Creates a tree of PHP objects from XML document
Descriptionobject xmltreestring str
&warn.experimental.func;
The function parses the XML document in
str and returns a tree PHP objects as the
parsed document. This function is isolated from the other functions,
which means you cannot access the tree with any of the other functions.
Modifying it, for example by adding nodes, makes no sense since there
is currently no way to dump it as an XML file.
However this function may be valuable if you want to read a file and
investigate the content.
domxml_root
Returns root element node
Descriptionobject domxml_rootobject doc
&warn.experimental.func;
domxml_root takes one argument, an object of class
"Dom document", and returns the root element node. There are actually
other possible nodes like comments which are currently disregarded.
The following example returns just the element with name CHAPTER and
prints it. The other root node -- the comment -- is not returned.
Retrieving root element
]>
Title
&sp;
a1b1c1a2c2a3b3c3";
if(!$dom = xmldoc($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}
$root = $dom->root();
/* or $root = domxml_root($dom); */
print_r($root);
?>
]]>
domxml_add_root
Adds a further root node
Descriptionresource domxml_add_rootresource docstring name
&warn.experimental.func;
Adds a root element node to a dom document and returns the new node.
The element name is given in the second parameter.
Creating a simple HTML document header
add_root("HTML");
$head = $root->new_child("HEAD", "");
$head->new_child("TITLE", "Hier der Titel");
echo $doc->dumpmem();
?>
]]>
domxml_dumpmem
Dumps the internal XML tree back into a string
Descriptionstring domxml_dumpmemresource doc
&warn.experimental.func;
Creates an XML document from the dom representation. This function
usually is called after a building a new dom document from scratch
as in the example of domxml_add_root.
See also domxml_add_rootdomxml_attributes
Returns an array of attributes of a node
Descriptionarray domxml_attributesresource node
&warn.experimental.func;
Returns all attributes of a node as array of objects of type
"dom attribute".
domxml_get_attribute
Returns a certain attribute of a node
Descriptionobject domxml_get_attributeresource nodestring name
&warn.experimental.func;
Returns the attribute with name name of the given
node.
See also domxml_set_attributedomxml_set_attributeDescriptionobject domxml_set_attributeresource nodestring namestring value
&warn.experimental.func;
Sets an attribute with name name of the given
node on a value.
If we take the example from domxml_add_root it
is simple to add an attribute to the HEAD element by the simply calling
the set_attribute function of the node class.
Adding an attribute to an element
add_root("HTML");
$head = $root->new_child("HEAD", "");
$head->new_child("TITLE", "Hier der Titel");
$head->set_attribute("Language", "ge");
echo $doc->dumpmem();
?>
]]>
domxml_children
Returns children of a node or document
Descriptionarray domxml_childrenobject doc|node
&warn.experimental.func;
Returns all children of a node as an array of nodes.
In the following example the variable children will
contain an array with one node of type XML_ELEMENT. This node is
the TITLE element.
Adding an attribute to an element
add_root("HTML");
$head = $root->new_child("HEAD", "");
$head->new_child("TITLE", "Hier der Titel");
$head->set_attribute("Language", "ge");
$children = $head->children();
?>
]]>
domxml_new_child
Adds new child node
Descriptionresource domxml_new_childstring namestring content
&warn.experimental.func;
Adds a new child of type element to a node and returns it.
domxml_new_xmldoc
Creates new empty XML document
Descriptionobject domxml_new_xmldocstring version
&warn.experimental.func;
Creates a new dom document from scratch and returns it.
See also domxml_add_rootxpath_new_context
Creates new xpath context
Descriptionobject xpath_new_contextobject dom document
&warn.experimental.func;
See also xpath_eval
Evaluates the XPath Location Path in the given string
Descriptionarray xpath_evalobject xpath context
&warn.experimental.func;
See also domxml_node
Creates node
Descriptionobject domxml_nodestring name
&warn.undocumented.func;
domxml_node_set_content
Sets content of a node
Descriptionbool domxml_node_set_contentstring content
&warn.undocumented.func;
domxml_node_unlink_node
Deletes node
Descriptionobject domxml_node_unlink_node
&warn.undocumented.func;
xpath_eval_expression
Evaluates the XPath Location Path in the given string
Descriptionarray xpath_evalobject xpath_context
&warn.experimental.func;
See also domxml_version
Get XML library version
Descriptionstring domxml_version
This function returns the version of the XML library version currently used.
xptr_new_context
Create new XPath Context
Descriptionstring xptr_new_contextobject doc_handle
&warn.undocumented.func;
xptr_eval
Evaluate the XPtr Location Path in the given string
Descriptionint xptr_evalobject xpath_contextstring eval_str
&warn.undocumented.func;