SAX-parsing XML weather data
Source code
1
<?php
2 require("./.config.php");
3 require('Cache/Lite/Output.php');
4 $cache = new Cache_Lite_Output( array(
5 'cacheDir' => './cache/', 'lifeTime' => 3600
6 ));
7 $id = 'ewr_weather_data';
8
9 if (!$cache->start($id)) {
10 // load parent class
11 require('XML/Parser.php');
12 //extend the class
13 class Weather_Parser extends XML_Parser {
14
15 // hold character data here
16 private $cdata;
17
18 // implement handers for opening & closing tags, and stuff in between
19 function startHandler($xp,$element,&$attribs) {
20 $this->cdata = '';
21 if ($element=='CURRENT_OBSERVATION') {
22 echo '<table border="1" cellpadding="2" cellspacing="0">';
23 return;
24 }
25 if ($element=='IMAGE') {return;}
26 echo "<td style=\"text-align:right;font-weight:bold\">$element </td>";
27 }
28
29 function cdataHandler($xp,$data) {
30 $this->cdata .= $data;
31 }
32
33 function endHandler($xp,$element) {
34 if ($element=='CURRENT_OBSERVATION') {echo '</table><br />';return;}
35 if ($element=='IMAGE') {return;}
36 print "<td>$this->cdata</td></tr>";
37 }
38 }
39 $ch = curl_init("http://weather.gov/data/current_obs/KEWR.xml");
40 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
41 $xml = curl_exec($ch);
42 curl_close($ch);
43 $parser = new Weather_Parser();
44 //$fh = fopen('./weather_data.xml','r') or die('shit');
45 //$parser->setInputFile($fh)
46 //$parser->parse();
47 $parser->parseString($xml);
48 $cache->end();
49 } else {
50 echo "<p>The whole thing came from the cache. Cool, n'est-ce pas?</p>";
51 }
52 include('./.footer.php')?>
Output
Warning: require(Cache/Lite/Output.php) [function.require]: failed to open stream: No such file or directory in /home/davidmintz/www/davidmintz.org/php_course/examples/10/SAX_weather_parser.php5 on line 3
Fatal error: require() [function.require]: Failed opening required 'Cache/Lite/Output.php' (include_path='/usr/home/dmintz/inc/pear:.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/davidmintz/www/davidmintz.org/php_course/examples/10/SAX_weather_parser.php5 on line 3