$dom = domxml_open_file('books.xml');
//Here's how to read XML from a variable:
$dom = domxml_open_mem($books);
$root = $dom->document_element( );
function process_node($node) { if ($node->has_child_nodes( )) { foreach($node->child_nodes( ) as $n) { process_node($n); } } // process leaves if ($node->node_type( ) = = XML_TEXT_NODE) { $content = rtrim($node->node_value( )); if (!empty($content)) { print "$content\n"; } } } process_node($root);
The W3C's DOM provides a platform- and language-neutral method
that specifies the structure and content of a document. Using the DOM, you can
read an XML document into a tree of nodes and then maneuver through the tree to
locate information about a particular element or elements that match your
criteria. This is called tree-based parsing . In contrast, the non-DOM XML
functions allow you to do event-based parsing.