note 80115 deleted from class.xmlreader by thiago

From: Date: Wed, 12 Nov 2008 13:42:47 +0000
Subject: note 80115 deleted from class.xmlreader by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146434@lists.php.net to get a copy of this message
Note Submitter: Matt Heidenreich ---- 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); } ?>

« previous php.notes (#146434) next »