Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM XML Functions

DOM XML Functions

Introduction

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.

Note:

This extension has been moved to the » PECL repository and is no longer bundled with PHP as of PHP 5.0.0.

Note:

This extension is no longer marked experimental. It will, however, never be released with PHP 5, and will only be distributed with PHP 4. If you need DOM XML support with PHP 5 you can use the DOM extension. This domxml extension is not compatible with the DOM extension.

Requirements

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

Installation

This » PECL extension is not bundled with PHP. Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/domxml.

In PHP 4 this PECL extensions source can be found in the ext/ directory within the PHP source or at the PECL link above. 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.

Windows users will enable php_domxml.dll inside of php.ini in order to use these functions. In PHP 4 this DLL resides in the extensions/ directory within the PHP Windows binaries download. The DLL for this PECL extension may be downloaded from either the » PHP Downloads page or from » http://pecl4win.php.net/ Also, there is one additional DLL that must be made available to your system's PATH in order for this extension to work. In PHP 4 this is in the dlls/ directory. It's name: For PHP <= 4.2.0, it's libxml2.dll. For PHP >= 4.3.0, it's iconv.dll. And as of PHP 5.0.0, iconv is compiled into your Windows PHP binaries by default so no extra DLL is needed.

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

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


Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

Table 68. XML constants

Constant Value Description
XML_ELEMENT_NODE (integer) 1 Node is an element
XML_ATTRIBUTE_NODE (integer) 2 Node is an attribute
XML_TEXT_NODE (integer) 3 Node is a piece of text
XML_CDATA_SECTION_NODE (integer) 4  
XML_ENTITY_REF_NODE (integer) 5  
XML_ENTITY_NODE (integer) 6 Node is an entity like &nbsp;
XML_PI_NODE (integer) 7 Node is a processing instruction
XML_COMMENT_NODE (integer) 8 Node is a comment
XML_DOCUMENT_NODE (integer) 9 Node is a document
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.

Table 69. 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  


Table 70. 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


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



Table 73. DomAttribute class (DomAttribute : DomNode)

Method name   Remark
name DomAttribute_name()  
value DomAttribute_value()  
specified DomAttribute_specified()  


Table 74. DomProcessingInstruction class (DomProcessingInstruction : DomNode)

Method name Function name Remark
target DomProcessingInstruction_target()  
data DomProcessingInstruction_data()  


Table 75. Parser class

Method name Function name Remark
add_chunk Parser_add_chunk()  
end Parser_end()  


Table 76. XPathContext class

Method name Function name Remark
eval XPathContext_eval()  
eval_expression XPathContext_eval_expression()  
register_ns XPathContext_register_ns()  


Table 77. 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 547. 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

Code Examples / Notes » ref.domxml

deborah dot seidman

You can always use a sax parser (expat) which saves on memory storage (there is none as sax is event driven) and use this neat code to produce an array structure of you xml file :
see http://fr2.php.net/manual/fr/function.xml-parse.php
comment by
tgrabietz at bupnet dot de
22-Sep-2004 05:05


sorn

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-


sam

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


bart

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
".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;
}
}
?>


ngc dontspamme rapanden spamoff dk

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>";
?>


01-dec-2006 04:05

Referenced array functions drive me crazy for one reason or another (personal issue I guess).  So for any others like me, here's my modification (thanks to the original posters below for the base to work on!)
I haven't tested this on much more than simple XML files, so there's probably a few ways to break this - I'm also thinking this could probably be rewritten to be more efficient also, but it's working quite well for me thus far.
<?php
function xml2array($domnode)
{
$nodearray = array();
$domnode = $domnode->firstChild;
while (!is_null($domnode))
{
$currentnode = $domnode->nodeName;
switch ($domnode->nodeType)
{
case XML_TEXT_NODE:
if(!(trim($domnode->nodeValue) == "")) $nodearray['cdata'] = $domnode->nodeValue;
break;
case XML_ELEMENT_NODE:
if ($domnode->hasAttributes() )
{
$elementarray = array();
$attributes = $domnode->attributes;
foreach ($attributes as $index => $domobj)
{
$elementarray[$domobj->name] = $domobj->value;
}
}
break;
}
if ( $domnode->hasChildNodes() )
{
$nodearray[$currentnode][] = xml2array($domnode);
if (isset($elementarray))
{
$currnodeindex = count($nodearray[$currentnode]) - 1;
$nodearray[$currentnode][$currnodeindex]['@'] = $elementarray;
}
} else {
if (isset($elementarray) && $domnode->nodeType != XML_TEXT_NODE)
{
$nodearray[$currentnode]['@'] = $elementarray;
}
}
$domnode = $domnode->nextSibling;
}
return $nodearray;
}
?>


squiz

Re: websiterepairguys...  Close but no cigar ;-)
As written it will not work if the repeated tags are somewhere other than the first node, i.e. the following will not work:
<nodes>
 <node>onething</node>
 <node>something</node>
 <node>something</node>
</nodes>
You must store the new node name when you get a new sibling that doesn't match the previous and then it will work OK.  Amended code:
function dom_to_array($domnode, &$array) {
 $parent=$domnode;
 $domnode = $domnode->firstChild;
 $myname=$domnode->nodeName;
 $x=1;
 while (!is_null($domnode)) {
    switch ($domnode->nodeType) {
       case XML_ELEMENT_NODE: {
          if ( !$domnode->hasChildNodes()) {
             $array[$domnode->nodeName]='';
          } else if ( $domnode->hasChildNodes() && $domnode->firstChild->nodeType==XML_TEXT_NODE) {
             $array[$domnode->nodeName]=$domnode->firstChild->nodeValue;
          } else if ( $domnode->hasChildNodes() )  {
             $array_ptr = & $array[$domnode->nodeName];
             dom_to_array($domnode, $array_ptr);
          }
          break;
       }
    }
    $domnode = $domnode->nextSibling;
    if($domnode->nodeName == $myname)
    {
       $domnode->nodeName.=($x++);      
    } else {
       $myname = $domnode->nodeName;
    }
 }
}


alex

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. http://alexandre.alapetite.net/doc-alex/domxml-php4-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.


palatinusz - karoly_szalkary

My short way of parsing an XML document, for example displaying the document in a structured form:
<?php
$indent = "";
$file = "semi.xml";
$showfile = file_get_contents("c:/Program Files/Apache Group/apache/htdocs/phpxml" . "/" . $file);  // whatever path
  // maybe the whole path is not important, look it up in other posts
$newstring=utf8_encode($showfile);          // it's important!
if(!$domDocument = domxml_open_mem($newstring)) {
  echo "Couldn't load xml...";    
  exit;
}
$rootDomNode = $domDocument->document_element();
print "<pre>";
printElements($rootDomNode);
print "</pre>";
function printElements($domNode)
{
 if($domNode)
 {
   global $indent;
   
   if($domNode->node_type() == XML_ELEMENT_NODE)
   {
     print "<br />".$indent."&lt;".$domNode->node_name();
     if($domNode->has_attributes())
     {
       $attributes = $domNode->attributes();
       foreach($attributes as $domAttribute)
       {
         print " $domAttribute->name=\"$domAttribute->value\"";
       }
     }
     print "&gt;";
     if($domNode->has_child_nodes())
     {
       $indent.="  ";
       $nextNode = $domNode->first_child();
       printElements($nextNode);
       $indent= substr($indent, 0, strlen($indent)-2);
       print "<br />".$indent."&lt;"."/".$domNode->node_name()."&gt;";
     }
     else
     {
       print "$domNode->node_value()&lt;/".$domNode->node_name()."&gt;";
     }
   }
   
   $nextNode = $domNode->next_sibling();
   printElements($nextNode);
 }
}
?>


nospam

If you're having trouble understanding how the the DOM XML extension fits together you may find the UML diagram here helps: http://www.phppatterns.com/index.php/article/articleview/38

timo dot hummel

If you want to subclass the domxml-classes, you have to use PHP5. It doesn't work with PHP4, and never will.

steve

If you are using apache, instead of copying files around (iconv.dll for instance) you can use this in your httpd.conf for apache:
LoadFile "d:/php/dlls/iconv.dll"
I placed this line before
LoadModule php4_module "d:/php/sapi/php4apache2.dll"
and it worked, no copying of files or anything therefore helps when updating php, don't have to mess around searching for files and other stuff.


bps7j

I've also rolled my own DOM-like functionality for places where the DOM extensions aren't available.
http://www.sequent.org/baron/script-hacking.php


websiterepairguys

I tried using the dom_to_simple_array that the user jas posted above, but it didnt work very well.
The problems were it didnt handle sibling nodes with the same name, such as:
<nodes>
 <node>something</node>
  <node>something</node>
</nodes>
Also, when it built child arrays from child nodes, it always interjected an wrapping array around the child, which isnt necessary.  Here is the patched code:
function dom_to_array($domnode, &$array) {
 $parent=$domnode;
 $domnode = $domnode->firstChild;
 $myname=$domnode->nodeName;
 $x=1;
 while (!is_null($domnode)) {
    switch ($domnode->nodeType) {
   
      case XML_ELEMENT_NODE: {
     
    if ( !$domnode->hasChildNodes()) {
    $array[$domnode->nodeName]='';
    } else if ( $domnode->hasChildNodes() && $domnode->firstChild->nodeType==XML_TEXT_NODE) {
    $array[$domnode->nodeName]=$domnode->firstChild->nodeValue;
    } else if ( $domnode->hasChildNodes() )  {
   
        $array_ptr = & $array[$domnode->nodeName];
      dom_to_array($domnode, $array_ptr);
        break;
      }
   }
   }
$domnode = $domnode->nextSibling;
  if($domnode->nodeName==$myname)
  {
    $domnode->nodeName.=($x++);  
  }
 }
}
snippet of array produced by this:
[admin] => Array
       (
           [menu] => Array
               (
                   [title] => Page Manager
                   [view] => list
               )
           [files] => Array
               (
                   [filename] => modules/testmodule/testmodule.php
                   [filename1] => modules/testmodule/testmodule.xml
                   [filename2] => media/lang/en-us/templates/testmodule.tpl
               )
       )


ndaniel

I recently developed a script for parsing DHL XML transaction responses - finding it a pain in the rear to actually parse the XML and set my variables - it actually wasn't that hard once I figured it out - and it goes something like this...
<?php
// Use with a class containing functions set_attributes() and
// set_data().  Use the following to set variables from the
// resulting xml.  $node is a dom xml object - in the first call
// to loop, $node would be equal to the root document
// element.
function loop($node) {
// set attribute tags here
if ($node->has_attributes()) {
$this->set_attributes($node);
} // end if node has attributes

if ($node->has_child_nodes()) {
$this->loop($node->first_child());
} // end if node has child
else {
$this->set_data($node);
} // end if node has no child
// get next sibling
$node = $node->next_sibling();
if ($node) {
$this->loop($node);
} // end if node
} // end function loop
?>
The code goes from the root element, if the element has attributes - it sets attribute variables.  Second, it recursively proceeds to the lowest level element (no more children).  Once that level has been reached, data variables are set.  The next step goes to the next sibling of the element, if it exists.  If the next sibling does not exists, the function is ended and the current element is returned to the parent element.  The parent element is then checked for siblings.  This process continues (as is with recursion) until the parent element is back at the root element, which is the end of the document.


jas

i needed to have an easy way to create a multi-dimensional but EXTREMELY SIMPLE php array out of some XML text i'm receiving. NOT an object. just an ARRAY.
i found that as simple a request as this seemed to be, the new (php5) DOM functions do not provide this functionality.
even the SimpleXML functions are object-oriented, which doesn't work for some of my purposes (sending to a Smarty template variable for looping through, etc.) -- returning attributes as SimpleXMLElement objects instead of strings, etc.. i just wanted an ARRAY containing the data as STRINGS.
eli (http://www.hoktar.com) had submitted such code earlier, based on domxml/php4 calls. his function was called "domxml_xmlarray".
but when php5 came out, eli's comments at the bottom of the PHP site got erased. (fortunately, i had already saved his code.) no doubt, mine will too w/next version..
furthermore, as far as i can tell, no one has taken the cue to add something like eli's domxml_xmlarray function directly into the DOMDocument object (but it would be nice).
so i translated eli's code, now using the dom calls (instead of the older domxml calls), and renamed the function to "dom_to_simple_array()".
below is a script containing the function itself as well as an example of its use. just copy it to your server somewhere and execute it and it should work right off the bat if you are using php5.
thanks.
jeff stern
 
==================================================================
<?php
function dom_to_simple_array($domnode, &$array) {
 $array_ptr = &$array;
 $domnode = $domnode->firstChild;
 while (!is_null($domnode)) {
   if (! (trim($domnode->nodeValue) == "") ) {
     switch ($domnode->nodeType) {
       case XML_TEXT_NODE: {
         $array_ptr['cdata'] = $domnode->nodeValue;
         break;
       }
       case XML_ELEMENT_NODE: {
         $array_ptr = &$array[$domnode->nodeName][];
         if ($domnode->hasAttributes() ) {
           $attributes = $domnode->attributes;
           if (!is_array ($attributes)) {
             break;
           }
           foreach ($attributes as $index => $domobj) {
             $array_ptr[$index] = $array_ptr[$domobj->name] = $domobj->value;
           }
         }
         break;
       }
     }
     if ( $domnode->hasChildNodes() ) {
       dom_to_simple_array($domnode, $array_ptr);
     }
   }
   $domnode = $domnode->nextSibling;
 }
}
 # now, let's make a sample string containing some XML
 $strXMLData = "<contacts>
      <contact>
        <name>
          John Doe
        </name>
        <phone>
          123-456-7890
        </phone>
      </contact>
      <contact>
        <name>
          Mary Smiley
        </name>
        <phone>
          567-890-1234
        </phone>
      </contact>
    </contacts>";
 # create a DOM tree xml object (hierarchical array) from
 # this XML string
 $domdoc = new DOMDocument;
 $domdoc->loadXML($strXMLData);
 # now simplify the DOM array into a very simple array structure
 # first, create an empty array to be filled with your
 # simplified array result..
 $aData = array();
 # now, pass the dom document and your empty array to the
 # converter function.
 dom_to_simple_array($domdoc, $aData);
 # now $aData contains your simplified array, so print it out
?><html>
<body>


there are <? echo count($aData['contacts'][0]['contact']); ?>
contacts


the 2nd contact's phone number is
<?echo $aData['contacts'][0]['contact'][1]['phone'][0]['cdata']; ?>

<hr />


Here is the raw array structure:
<pre>
<? print_r($aData); ?>
</pre>
</body>
</html>
==================================================================


gue-gue

Hi at All,
if you use xpath_eval() you get a xpathobject with a type-member-variable, which tells you about the type of the found content. Here are the values and the corresponding types:
1 = XPATH_NODESET (integer)
2 = XPATH_BOOLEAN (integer)
3 = XPATH_NUMBER (integer)
4 = XPATH_STRING (integer)
I think, but don't know, that the rest of the constants are:
0 = XPATH_UNDEFINED (integer)      
5 = XPATH_POINT (integer)      
6 = XPATH_RANGE (integer)      
7 = XPATH_LOCATIONSET (integer)
I hope i could help some people.
Greetz,
Chris


bradparks

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)
http://www.engageinteractive.com/redir.php?resource=3&target=domit
or
Saxy, a sax based xml parser
http://www.engageinteractive.com/redir.php?resource=4&target=saxy
Brad


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker