note 37342 added to ref.domxml

From: Date: Tue, 11 Nov 2003 16:46:06 +0000
Subject: note 37342 added to ref.domxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-60251@lists.php.net to get a copy of this message
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<br>".str_repeat("&nbsp;", ($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 '&lt;', $ChildDomNode->node_name(); if ($ChildDomNode->has_attributes()): $Array = $ChildDomNode->attributes(); foreach ($Array AS $DomAttribute): echo ' ', $DomAttribute->name(), '="', $DomAttribute->value(), '"'; endforeach; endif; echo '&gt;'; if ($ChildDomNode->has_child_nodes()): $depth++; if (PrintDomTree($ChildDomNode)): echo $whitespace; endif; $depth--; endif; echo '&lt;/', $ChildDomNode->node_name(), '&gt;'; endif; $ChildDomNode = $ChildDomNode->next_sibling(); endwhile; return $HasTag; endif; } $DomDocument = domxml_open_mem($xmlstr); $RootDomNode = $DomDocument->document_element(); PrintDomTree($RootDomNode); ?> ---- Manual Page -- http://www.php.net/manual/en/ref.domxml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37342 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37342&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37342&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60251) next »