One of the major advantages of the DOM is that by following the
W3C's specification, many languages implement DOM
functions in a similar manner. Therefore, the work of translating
logic and instructions from one application to another is
considerably simplified. PHP 4.3 comes with an updated series of DOM
functions that are in stricter compliance with the DOM standard than
previous versions of PHP. However, the functions are not yet 100%
compliant. Future PHP versions should bring a closer alignment, but
this may break some applications that need minor updates. Check the
DOM
XML material in the online PHP Manual at
http://www.php.net/domxml for changes. Functions
available in earlier versions of PHP are available, but deprecated.
<books>
<book>
<title>PHP Cookbook</title>
<author>Sklar</author>
<author>Trachtenberg</author>
<subject>PHP</subject>
</book>
<book>
<title>Perl Cookbook</title>
<author>Christiansen</author>
<author>Torkington</author>
<subject>Perl</subject>
</book>
</books>
Here's how to find all authors:
// find and print all authors
$authors = $dom->get_elements_by_tagname('author');
// loop through author elements
foreach ($authors as $author) {
// child_nodes( ) hold the author values
$text_nodes = $author->child_nodes( );
foreach ($text_nodes as $text) {
print $text->node_value( );
}
print "\n";
}
The get_elements_by_tagname( ) function returns an
array of element node objects. By looping through each
element's children, you can get to the text node
associated with that element. From there, you can pull out the node
values, which in this case are the names of the book authors, such as
Sklar and Trachtenberg.