XML_Parser -> Slightly more complicated example with func mode parsing

From: Date: Mon, 02 May 2005 18:39:02 +0000
Subject: XML_Parser -> Slightly more complicated example with func mode parsing
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-19046@lists.php.net to get a copy of this message
Hi Folks, I was looking through the CVS example for the XML_Parser class and was looking for something just a little more complicated. While I've had lots of experiencing generating XML, applying stylesheet transformations to them, I am still new to the whole parsing game. A sample xml code fragment: <georeference> <continent id="North America"> <country id="Canada"> <region id="AB"> <site id="AB-12"> <name>Airdrie</name> </side> </region> </country> </continent> </georeference> I was looking to roll up and provide a list of the entire name & site id combinations for a particular reigon. Example: All of the "AB-12" & "Airdie" combinations for the province of "AB". I thought this might be best done by setting the mode to "func" and created the xmltag_region() and xmltag_site() functions. Here's my php code snippet: <?php /** * Weather xml parser class used for parsing the Environment Canada weather * xml file * * @author Stephen March <steve@enerds.ca> * @file weather.class.php, v1.0 2005/05/02 * @package Weather **/ require_once "XML/Parser.php"; class Weather extends XML_Parser { /** * Call the Superclass constructor **/ function Weather() { parent::XML_Parser(); } /** * Function for the SITE tag. Retrieve the site name as well as the code **/ function xmltag_site($xp, $name, $attribs) { print $attribs["ID"] . "<br/>"; } /** * Function for the REGION tag. Retrieve the region name and all city codes * for the region **/ function xmltag_region($xp, $name, $attribs) { print $attribs["ID"] . "<br/>"; } } /** * Example useage of the Weather class **/ $w = &new Weather(); $w->setMode('func'); $result = $w->setInputFile("canada_e.xml"); if($w->parse()) { print "Successfully parsed!"; } else { print "Could not parse file!"; } ?>

« previous php.pear.general (#19046) next »