PHP: DOM XML Functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<DOMXPath->registerNamespaceDomAttribute->name>
view the version of this page
Last updated: Thu, 15 Jul 2004

XXV. DOM XML Functions

�vod

Varov�n�

Toto roz���en� je EXPERIMENT�LN�. Chov�n� tohoto roz���en�, n�zvy funkc� a v�echno ostatn�, co je zde zdokumentov�no, se v budouc�ch verz�ch PHP m��e bez ohl�en� zm�nit. Berte to v �vahu a pou��vejte tento modul na vlastn� nebezpe��.

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.

Po�adavky

This extension makes use of the . Download and install this library. You will need at least libxml-2.4.14. To use DOM XSLT features you can use the and EXSLT enhancements from . Download and install these libraries if you plan to use (enhanced) XSLT features. You will need at least libxslt-1.0.18.

Instalace

This extension is only available if PHP was configured with --with-dom[=DIR]. Add --with-dom-xslt[=DIR] to include DOM XSLT support. DIR is the libxslt install directory. Add --with-dom-exslt[=DIR] to include DOM EXSLT support, where DIR is the libexslt install directory.

Note to Win32 Users: In order to enable this module on a Windows environment, you must copy one additional file from the DLL folder of the PHP/Win32 binary package to the SYSTEM32 folder of your Windows machine (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM32). For PHP <= 4.2.0 copy libxml2.dll, for PHP >= 4.3.0 copy iconv.dll from the DLL folder to your SYSTEM32 folder.

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().

Tabulka 1. Deprecated functions and their replacements

Old functionNew function
xmldocdomxml_open_mem()
xmldocfiledomxml_open_file()
domxml_new_xmldocdomxml_new_doc()
domxml_dump_memDomDocument_dump_mem()
domxml_dump_mem_fileDomDocument_dump_file()
DomDocument_dump_mem_fileDomDocument_dump_file()
DomDocument_add_rootDomDocument_create_element() followed by DomNode_append_child()
DomDocument_dtdDomDocument_doctype()
DomDocument_rootDomDocument_document_element()
DomDocument_childrenDomNode_child_nodes()
DomDocument_imported_nodeNo replacement.
DomNode_add_childCreate a new node with e.g. DomDocument_create_element() and add it with DomNode_append_child().
DomNode_childrenDomNode_child_nodes()
DomNode_parentDomNode_parent_node()
DomNode_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().

P�eddefinovan� konstanty

Tyto konstanty jsou definov�ny t�mto roz���en�m a budou k dispozici pouze tehdy, bylo-li roz���en� zkompilov�no spole�n� s PHP nebo dynamicky zavedeno za b�hu.

Tabulka 2. XML konstanty

KonstantaHodnotaPopis
XML_ELEMENT_NODE (integer) 1Uzel je element
XML_ATTRIBUTE_NODE (integer) 2Uzel je atribut
XML_TEXT_NODE (integer) 3Uzel je kus textu
XML_CDATA_SECTION_NODE (integer) 4 
XML_ENTITY_REF_NODE (integer) 5 
XML_ENTITY_NODE (integer) 6Uzel je entita jako &nbsp;
XML_PI_NODE (integer) 7Uzel je instrukce zpracov�n�
XML_COMMENT_NODE (integer) 8Uzel je koment��
XML_DOCUMENT_NODE (integer) 9Uzel je dokument
XML_DOCUMENT_TYPE_NODE (integer) 10 
XML_DOCUMENT_FRAG_NODE (integer) 11 
XML_NOTATION_NODE (integer) 12 
XML_GLOBAL_NAMESPACE (integer) 1 
XML_LOCAL_NAMESPACE (integer) 2 
XML_HTML_DOCUMENT_NODE (integer)   
XML_DTD_NODE (integer)   
XML_ELEMENT_DECL_NODE (integer)   
XML_ATTRIBUTE_DECL_NODE (integer)   
XML_ENTITY_DECL_NODE (integer)   
XML_NAMESPACE_DECL_NODE (integer)   
XML_ATTRIBUTE_CDATA (integer)   
XML_ATTRIBUTE_ID (integer)   
XML_ATTRIBUTE_IDREF (integer)   
XML_ATTRIBUTE_IDREFS (integer)   
XML_ATTRIBUTE_ENTITY (integer)   
XML_ATTRIBUTE_NMTOKEN (integer)   
XML_ATTRIBUTE_NMTOKENS (integer)   
XML_ATTRIBUTE_ENUMERATION (integer)   
XML_ATTRIBUTE_NOTATION (integer)   
XPATH_UNDEFINED (integer)   
XPATH_NODESET (integer)   
XPATH_BOOLEAN (integer)   
XPATH_NUMBER (integer)   
XPATH_STRING (integer)   
XPATH_POINT (integer)   
XPATH_RANGE (integer)   
XPATH_LOCATIONSET (integer)   
XPATH_USERS (integer)   
XPATH_NUMBER (integer)   

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.

Tabulka 3. List of classes

Class nameParent classes
DomAttributeDomNode
DomCDataDomNode
DomCommentDomCData : DomNode
DomDocumentDomNode
DomDocumentTypeDomNode
DomElementDomNode
DomEntityDomNode
DomEntityReferenceDomNode
DomProcessingInstructionDomNode
DomTextDomCData : DomNode
ParserCurrently still called DomParser
XPathContext 

Tabulka 4. DomDocument class (DomDocument : DomNode)

Method nameFunction nameRemark
doctypeDomDocument_doctype() 
document_elementDomDocument_document_element() 
create_elementDomDocument_create_element() 
create_text_nodeDomDocument_create_text_node() 
create_commentDomDocument_create_comment() 
create_cdata_sectionDomDocument_create_cdata_section() 
create_processing_instructionDomDocument_create_processing_instruction() 
create_attributeDomDocument_create_attribute() 
create_entity_referenceDomDocument_create_entity_reference() 
get_elements_by_tagnameDomDocument_get_elements_by_tagname() 
get_element_by_idDomDocument_get_element_by_id() 
dump_memDomDocument_dump_mem()not DOM standard
dump_fileDomDocument_dump_file()not DOM standard
html_dump_memDomDocument_html_dump_mem()not DOM standard
xpath_initxpath_initnot DOM standard
xpath_new_contextxpath_new_contextnot DOM standard
xptr_new_contextxptr_new_contextnot DOM standard

Tabulka 5. DomElement class (DomElement : DomNode)

Method nameFunction nameRemark
tagnameDomElement_tagname() 
get_attributeDomElement_get_attribute() 
set_attributeDomElement_set_attribute() 
remove_attributeDomElement_remove_attribute() 
get_attribute_nodeDomElement_get_attribute_node() 
get_elements_by_tagnameDomElement_get_elements_by_tagname() 
has_attributeDomElement_has_attribute() 

Tabulka 7. DomAttribute class (DomAttribute : DomNode)

Method name Remark
nameDomAttribute_name() 
valueDomAttribute_value() 
specifiedDomAttribute_specified() 

Tabulka 8. DomProcessingInstruction class (DomProcessingInstruction : DomNode)

Method nameFunction nameRemark
targetDomProcessingInstruction_target() 
dataDomProcessingInstruction_data() 

Tabulka 9. Parser class

Method nameFunction nameRemark
add_chunkParser_add_chunk() 
endParser_end() 

Tabulka 10. XPathContext class

Method nameFunction nameRemark
evalXPathContext_eval() 
eval_expressionXPathContext_eval_expression() 
register_nsXPathContext_register_ns() 

Tabulka 11. DomDocumentType class (DomDocumentType : DomNode)

Method nameFunction nameRemark
nameDomDocumentType_name() 
entitiesDomDocumentType_entities() 
notationsDomDocumentType_notations() 
public_idDomDocumentType_public_id() 
system_idDomDocumentType_system_id() 
internal_subsetDomDocumentType_internal_subset() 

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

P��klady

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().

P��klad 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'>
  &amp;sp;
  <!-- comment -->
  <informaltable ID='findme' language='&amp;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>"
;
?>

Obsah
DomAttribute->name --  Returns name of 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 -- 
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 -- 
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 system id of document type
DomElement->get_attribute_node --  Returns value of attribute
DomElement->get_attribute --  Returns value of attribute
DomElement->get_elements_by_tagname --  Gets elements by tagname
DomElement->has_attribute --  Checks to see if attribute exists
DomElement->remove_attribute --  Removes attribute
DomElement->set_attribute --  Adds new attribute
DomElement->tagname --  Returns name of element
DomNode->add_namespace --  Adds a namespace declaration to a node.
DomNode->append_child --  Adds 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 data of pi node
DomProcessingInstruction->target --  Returns target of pi 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 -- Vytvo�it DOM objekt z XML souboru
domxml_open_mem -- Vytvo�it DOM objekt z XML dokumentu
domxml_version --  Get XML library version
domxml_xmltree -- Vytvo�it strom PHP objekt� z XML dokumentu
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 XML document in a string.
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
xptr_eval --  Evaluate the XPtr Location Path in the given string
xptr_new_context --  Create new XPath Context


add a note add a note User Contributed Notes
DOM XML Functions
Alex
04-Jul-2004 04:48
PHP4/DOMXML code is not compatible with the new PHP5/dom extension. While the conversion is quite strait forward, it can take a long time if domxml has been broadly used. Moreover, it can be interesting to have old PHP4 scripts ready for PHP5 as soon as possible even if the server is still running PHP4. Since I have that kind of problem, if have written a small library to include in PHP4 scripts to enable them to be run on PHP5.

It does not cover all the domxml functionality, but most of the main functions and can easily be extended. Tested with PHP4.3.7 and PHP5.0.0RC3 but I will try to keep it updated. I hope it can help.
sn00b at yahoo dot com
23-Jun-2004 11:18
When you want to use XSLT extensions you have to copy "expat.dll" and "sablot.dll" into your windows-system directory.
closedfile
21-Jun-2004 05:01
If you use IIS and Windows NT, pleace make sure that the file
"iconv.dll" is in the directory "winnt/system32/". Else you will get an error.
SAM
22-Apr-2004 06:05
When installing PHP --with-dom and --with-dom-xslt on a Red Hat 9.0 remember to install the following packages:

libxml
libxml2
libxml2-devel
libxslt
libxslt-devel

Then you will be spared error messages when trying to configure.

regards
SAM
ngc(dontSpamMe)rapanden(SpamOFF)dk
28-Jan-2004 12:48
Sorry, a bug in my code... I made the first version late at night, sorry!

The bug was in the "if ($ChildDomNode->has_child_nodes())" block, I didn't save the data for the for the CildNode of the CildNodes. the bug has been fixed.

<?php
function getElementAttributes($DomNode,$elementName,$attriName)
{
   if (
$ChildDomNode = $DomNode->first_child())
   {
       while(
$ChildDomNode)
       {
           if (
$ChildDomNode->node_type() == XML_ELEMENT_NODE)
             {
               if(
$ChildDomNode->node_name() == $elementName)
               {
              
                 if (
$ChildDomNode->has_attributes())
                 {
                  
$Array = $ChildDomNode->attributes();
                   foreach (
$Array AS $DomAttribute)
                   {
                       if(
$DomAttribute->name() == $attriName)
                       {
                        
$nodeArray[] = $DomAttribute->value();
                       }
                   }
// foreach ($Array AS $DomAttribute)

                
}//if ($ChildDomNode->has_attributes())
              
              
}
          
             if (
$ChildDomNode->has_child_nodes())
               {
                  
$tmpArray = (getElementAttributes($ChildDomNode,$elementName,$attriName));
                  
$nodeArray = array_merge($nodeArray, $tmpArray);
                   unset(
$tmpArray);
               }
// if ($ChildDomNode->has_child_nodes())
              
          
}//if ($ChildDomNode->node_type() == XML_ELEMENT_NODE)
          
          
$ChildDomNode = $ChildDomNode->next_sibling();

       }
//while($ChildDomNode)
      
  
return $nodeArray;
  }
//if ($ChildDomNode = $DomNode->first_child())
 
}
$file = "test3.xml";
$element = "pb";
$att = "id";
$DomDocument = domxml_open_file($file);
$RootDomNode = $DomDocument->document_element();
$array = getElementAttributes($RootDomNode,$element,$att);
echo
"<pre>";
print_r($array);
echo
"</pre>";
?>
bps7j at yahoSPAMo.com
24-Dec-2003 08:19
I've also rolled my own DOM-like functionality for places where the DOM extensions aren't available.

bradparks at bradparks dot com
17-Dec-2003 10:41
Hey;

If you need to parse XML on an older version of PHP (e.g. 4.0) or if you can't get the expat extension enabled on your server, you might want to check out the Saxy and DOMIT! xml parsers from Engage Interactive. They're opensource and pure php, so no extensions or changes to your server are required. I've been using them for over a month on some projects with no problems whatsoever!

Check em out at:

DOMIT!, a DOM based xml parser, uses Saxy (included)


or

Saxy, a sax based xml parser


Brad
bart at mediawave dot nl
11-Nov-2003 07:46
This recursive function will iterate over a DOM object and display it as a nicely formatted XML structure. I used intuitive variable names to help learn more about the DOM functions and their return values.

<<?php

function PrintDomTree($DomNode)
{
   if (
$ChildDomNode = $DomNode->first_child()) {
       static
$depth = 0;

      
$whitespace = "\n<br>".str_repeat(" ", ($depth * 2));

       while (
$ChildDomNode) {
           if (
$ChildDomNode->node_type() == XML_TEXT_NODE) {
               echo
trim($ChildDomNode->node_value());
           } elseif (
$ChildDomNode->node_type() == XML_ELEMENT_NODE) {
              
$HasTag = 1;
               echo
$whitespace;
               echo
"<", $ChildDomNode->node_name();

               if (
$ChildDomNode->has_attributes()) {
                  
$Array = $ChildDomNode->attributes();
                   foreach (
$Array AS $DomAttribute) {
                       echo
" ", $DomAttribute->name(), "=\"", $DomAttribute->value(), "\"";
                   }
               }
               echo
">";

               if (
$ChildDomNode->has_child_nodes()) {
                  
$depth++;
                   if (
PrintDomTree($ChildDomNode)) {
                       echo
$whitespace;
                   }
                  
$depth--;
               }
               echo
"</", $ChildDomNode->node_name(), ">";
           }
          
$ChildDomNode = $ChildDomNode->next_sibling();
       }
       return
$HasTag;
   }
}

?>
nospam at phppatterns dot com
09-Apr-2003 08:51
If you're having trouble understanding how the the DOM XML extension fits together you may find the UML diagram here helps:
sorn at dsl dot upc dot es
17-Mar-2003 11:38
When parsing "iso-8859-1" encoded XML files, use "utf8_decode" to recover node contents (libxml uses "UTF-8" internal encoding, so conversion needed).

--- BEGIN: mydata.xml ---
<?xml version="1.0" encoding="iso-8859-1"?>
...
--- END: mydata.xml---

--- BEGIN: myparser.php ---
<?php
...
$domxml = domxml_open_file("mydata.xml"));
...
$content = utf8_decode(trim($node->content));
echo
$content;
...
?>
--- END: myparser.php

-eof-
Daniel dot Veillard at w3 dot org
12-Aug-2000 06:46
I'm libxml maintainer, I suggest anybody using
the DOM functions to:
  - not stay with 2.0.x versions, upgrade please
    
  - have a look at xmlsoft.org if you want more
     information, but this might be misleading too
     since PHP-DOM doesn't expose all the
     libxml API
  - for the DOM reference
  - if you have trouble when parsing an
   XML/HTML files, check first that they are
     correct (xmllint distributed with libxml may
     help), if no fix them, if yes send me a bug
     report with the input exhibiting the problem

 happy DOM hacking,

[email protected]

<DOMXPath->registerNamespaceDomAttribute->name>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page