For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2005 > XML_Parser -> Slightly more complicated example with func mode parsing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML_Parser -> Slightly more complicated example with func mode parsing
Stephen March

2005-05-02, 8:57 pm

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!";

}



?>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com