This article mainly introduces the PHP method for traversing all nodes in the XML document. The example analyzes the skills of php in operating XML files and has some reference value, for more information about how to use PHP to traverse all nodes in the XML document, see the following example. Share it with you for your reference. The specific implementation method is as follows:
1. contact. xml code:
J
J
J
Manager
National
1971-12-22
2. php code:
<?php function walk_tree ($node, $depth = 0) { for ($i = 0, $indent = ''; $i < $depth; $i++) $indent .= ' '; if ($node->type == XML_ELEMENT_NODE) { print ($indent . $node->tagname . "\n"); $kids = $node->children (); $nkids = count ($kids); if ($nkids > 0) { $depth++; for ($i = 0; $i < $nkids; $i++) walk_tree ($kids[$i], $depth); $depth--; } } } $doc = xmldocfile ('contact.xml'); print ("\n"); walk_tree ($doc->root ()); print ("\n");?>
I hope this article will help you with php programming.