note 80115 added to ref.xmlreader

From: Date: Sun, 30 Dec 2007 09:33:07 +0000
Subject: note 80115 added to ref.xmlreader
Groups: php.notes 
Request: Send a blank email to php-notes+get-134276@lists.php.net to get a copy of this message
Here is an example of a recursive function to convert a multi-level XML file into an unordered list. left_nav.xml: <root> <item name="Contact Information" url="some_url" /> <item name="Directions" url="some_url" /> <item name="Map" url="some_url" /> <item name="Links of Interest"> <item name="Labs/Centers/ Institutes" url="some_url" /> <item name="Journals/Societies" url="some_url" /> <item name="Other"> <item name="Brain Imaging" url="some_url" /> <item name="ASL" url="some_url" /> <item name="Miscellaneous" url="some_url" /> </item> </item> <item name="Gallery" url="some_url" /> </root> <?php $xml = new XMLReader(); $xml->open("left_nav.xml"); parseXML($xml); function parseXML($node) { if (!$node->read()) return; //If this is the root node, then just continue down the tree. if ($node->nodeType==1 && $node->value == "root") parseXML($node); //If this is a text node then test for attributes. if ($node->nodeType==1 && $node->hasAttributes) { if ($node->getAttribute("url") != "") { print "<li>"; print "<a href=\"" . $node->getAttribute("url") . "\">" . $node->getAttribute("name") . "</a>"; print "</li>\n"; } else { print "<li>"; print $node->getAttribute("name"); print "</li>\n"; } } //If it is the beginning node then start the list. if ($node->nodeType==1 && !$node->isEmptyElement) print "<ul>\n"; //If it is the end node then close the list. if ($node->nodeType==15) print "</ul>\n"; //Call the recursive method again. parseXML($node); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 63.225.85.148 ---- Manual Page -- http://www.php.net/manual/en/ref.xmlreader.php Edit -- https://master.php.net/note/edit/80115 Del: integrated -- https://master.php.net/note/delete/80115/integrated Del: useless -- https://master.php.net/note/delete/80115/useless Del: bad code -- https://master.php.net/note/delete/80115/bad+code Del: spam -- https://master.php.net/note/delete/80115/spam Del: non-english -- https://master.php.net/note/delete/80115/non-english Del: in docs -- https://master.php.net/note/delete/80115/in+docs Del: other reasons-- https://master.php.net/note/delete/80115 Reject -- https://master.php.net/note/reject/80115 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134276) next »