DOM XML functions DOM 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 constants Constant Value Description XML_ELEMENT_NODE 1 Node is an element XML_ATTRIBUTE_NODE 2 Node is an attribute XML_TEXT_NODE 3 Node is a piece of text XML_CDATA_SECTION_NODE 4 XML_ENTITY_REF_NODE 5 XML_ENTITY_NODE 6 Node is an entity like   XML_PI_NODE 7 Node is a processing instruction XML_COMMENT_NODE 8 Node is a comment XML_DOCUMENT_NODE 9 Node is a document XML_DOCUMENT_TYPE_NODE 10 XML_DOCUMENT_FRAG_NODE 11 XML_NOTATION_NODE 12 XML_GLOBAL_NAMESPACE 1 XML_LOCAL_NAMESPACE 2
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 name Function name Description root domxml_root children domxml_children add_root domxml_add_root dtd domxml_intdtd dumpmem domxml xpath_init xpath_init xpath_new_context xpath_new_context xptr_new_context xptr_new_context
DomDocument class (attributes) Name Type Description doc class DomDocument The object itself name string url string version string Version of XML encoding string standalone long 1 if the file is a standalone version type long One of the constants in table ... compression long 1 if the file is compressed charset long
DomNode class (methods) Name PHP name Description lastchild domxml_last_child children domxml_children parent domxml_parent new_child domxml_new_child get_attribute domxml_get_attribute set_attribute domxml_set_attribute attributes domxml_attributes node domxml_node set_content domxml_set_content
DomNode class (attributes) Name Type Description node class DomNode The object itself type long name string content string
xmldoc Creates a DOM object of an XML document Description object xmldoc string 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 xmldocfile xmldocfile Creates a DOM object from XML file Description object xmldocfile string 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 xmldoc xmltree Creates a tree of PHP objects from XML document Description object xmltree string 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 Description object domxml_root object 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; a1b1c1 a2c2 a3b3c3 "; 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 Description resource domxml_add_root resource doc string 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 Description string domxml_dumpmem resource 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_root domxml_attributes Returns an array of attributes of a node Description array domxml_attributes resource 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 Description object domxml_get_attribute resource node string name &warn.experimental.func; Returns the attribute with name name of the given node. See also domxml_set_attribute domxml_set_attribute Description object domxml_set_attribute resource node string name string 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 Description array domxml_children object 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 Description resource domxml_new_child string name string 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 Description object domxml_new_xmldoc string version &warn.experimental.func; Creates a new dom document from scratch and returns it. See also domxml_add_root xpath_new_context Creates new xpath context Description object xpath_new_context object dom document &warn.experimental.func; See also xpath_eval Evaluates the XPath Location Path in the given string Description array xpath_eval object xpath context &warn.experimental.func; See also domxml_node Creates node Description object domxml_node string name &warn.undocumented.func; domxml_node_set_content Sets content of a node Description bool domxml_node_set_content string content &warn.undocumented.func; domxml_node_unlink_node Deletes node Description object domxml_node_unlink_node &warn.undocumented.func; xpath_eval_expression Evaluates the XPath Location Path in the given string Description array xpath_eval object xpath_context &warn.experimental.func; See also domxml_version Get XML library version Description string domxml_version This function returns the version of the XML library version currently used. xptr_new_context Create new XPath Context Description string xptr_new_context object doc_handle &warn.undocumented.func; xptr_eval Evaluate the XPtr Location Path in the given string Description int xptr_eval object xpath_context string eval_str &warn.undocumented.func;