PHP offers two methods of parsing an XML document. You've already seen how SAX works; in this article, find out how to use PHP's DOM functions to traverse an XML tree, and also learn about important differences between the two approaches.
As before, the first order of business is to read the XML data into memory. PHP offers three functions to do this: the xmldoc() and xmltree() functions accept a string containing XML data as argument, and build a tree structure from that string, while the xmldocfile() function accepts a filename as argument, and builds a DOM tree after reading the data in the file.
<?
// create an XML-compliant string
$XMLDataString = "<?xml version="1.0"?"
. "><me><name>Joe
Cool</name><age>24</age><sex>male</sex></me>";
//
create a document object
$XMLDoc = xmldoc($XMLDataString);
// create a tree object
$XMLTree
= xmltree($XMLDataString);
?>
You can also load a file directly.
<?
// data file
$XMLFile = "me.xml";
// create a document object
$XMLDoc
= xmldocfile($XMLFile);
?>
Windows users should note that xmldocfile() needs the complete path to the file,
including drive letter, to work correctly.{mospagebreak title=Parents And Their Children} Once the document has been read in, a number of methods become available to traverse the document tree.
If you're using a document object, such as the one returned by xmldoc() and xmldocfile(), you can use the following methods to obtain references to other nodes in the tree,
<?
// data file
$file = "library.xml";
// create a document object
$dom
= xmldocfile($file);
// echo these values to see the object type
// get root
node
$dom->root();
// get children under the root node as array
$dom->children();
?>
or to properties of the document itself.
<?
// data file
$file = "library.xml";
// create a document object
$dom
= xmldocfile($file);
// get XML version
echo $dom->version;
// get XML encoding
echo
$dom->encoding;
// get whether standalone file
echo $dom->standalone;
// get
XML URL
echo $dom->url;
// get XML character set
echo $dom->charset;
?>
Once you've obtained a reference to a node, a number of other methods and properties
become available to help you obtain the name and value of that node, as well as references to parent and child nodes. Take a look:
<?
// data file
$str = "<?xml version="1.0"?><me><name>Joe
Cool</name><age>24</age><sex>male</sex></me>";
//
create a document object
$dom = xmldoc($str);
// get reference to root node
$root
= $dom->root();
// get name of node - "me"
echo $root->name;
// get children
of node, as array
$children = $root->children();
// get first child node
$firstChild
= $children[0];
// let's see a few node properties
// get name of first child
node - "name"
echo $firstChild->name;
// get content of first child node - "Joe
Cool"
echo $firstChild->content;
// get type of first child node - "1", or "XML_ELEMENT_NODE"
echo
$firstChild->type;
// go back up the tree!
// get parent of child - this should
be <me>
$parentNode = $firstChild->parent();
// check it...yes, it is "me"!
echo
$parentNode->name;
?>
A quick note on the "type" property above: every node is of a specific type,
and this property returns a numeric and textual code corresponding to the type. A complete list of types is available in the PHP manual.{mospagebreak title=Welcome To The Human Race} Many of the object methods you just saw are also available as regular functions; for example,
<?
// data file
$str = "<?xml version="1.0"?><me><name>Joe
Cool</name><age>24</age><sex>male</sex></me>";
//
create a document object
$dom = xmldoc($str);
// get reference to root node
$root
= $dom->root();
// get name of node - "me"
echo $root->name;
// you could also
do this!
// get reference to root node
$root = domxml_root($dom);
// get name
of node - "me"
echo $root->name;
?>
Finally, element attributes may be obtained using either the $node->attributes()
object method, or the dom_attributes() function.
<?
// data file
$str = "<?xml version="1.0"?><me species="human"><name>Joe
Cool</name><age>24</age><sex>male</sex></me>";
//
create a document object
$dom = xmldoc($str);
// get reference to root node
$root
= $dom->root();
// get attribute collection
// $attributes = $root->attributes();
//
you could also do this!
// $attributes = domxml_attributes($root);
// get first
attribute name - "species"
echo $attributes[0]->name;
// get first attribute
value - "human"
echo $attributes[0]->children[0]->content;
// you could also
do this!
// echo domxml_getattr($root, "species");
?>