function getWeather($zip) { if (!$zip) { $zip = '02111'; } $file = file_get_contents('http://rss.weather.com/weather/rss/local/' . $zip . '?cm_ven=LWO&cm_cat=rss&par=LWO_rss'); $fs = new FileStorage($this->cache, FS_WRITE); $fs->write($file); $error = XMLParseFile($parser, $this->cache, 0); if ($error) { return $error; } else { $ret = array(); foreach ($parser->structure as $k => $v) { if (is_string($v) && strstr($v, 'For more') !== FALSE) { $v = str_replace('°', '°', $v); if (strstr($v, '---') !== FALSE) { $days = explode('---', $v); $junk = array_pop($days); $ret['forecast'] = $days; } else { $v = strip_tags($v); $ret['current'] = substr($v, 0, strpos($v, 'For more') - 1); } } if (count($ret) == 2) { break; } } } return $ret; }
function Parse($filename) { $this->header = array(); $this->footer = array(); $this->filename = $filename; $error = XMLParseFile($this->parser, $this->filename, 0, "", 1, "UTF-8"); //"ISO-8859-1"); if (strcmp($error, "")) { print "Parser Error: {$error}\n"; return $error; } else { $this->WalkXML("0"); //&$this->parser->structure, &$this->parser->positions); return false; } }
} } } function DumpStructure(&$structure, &$positions, $path) { echo "[" . $positions[$path]["Line"] . "," . $positions[$path]["Column"] . "," . $positions[$path]["Byte"] . "]"; if (GetType($structure[$path]) == "array") { echo "<" . $structure[$path]["Tag"] . ">"; for ($element = 0; $element < $structure[$path]["Elements"]; $element++) { DumpStructure($structure, $positions, $path . ",{$element}"); } echo "</" . $structure[$path]["Tag"] . ">"; } else { echo $structure[$path]; } } $file_name = "example.xml"; $error = XMLParseFile($parser, $file_name, 1, $file_name . ".cache"); if (strcmp($error, "")) { echo "<H2><CENTER>Parser error: {$error}</CENTER></H2>\n"; } else { echo "<H2><CENTER>Parsed file structure</CENTER></H2>\n"; echo "<P>This example dumps the structure of the elements of an XML file by displaying the tags and data preceded by their positions in the file: line number, column number and file byte index.</P>\n"; echo "<PRE>"; DumpStructure($parser->structure, $parser->positions, "0"); echo "</PRE>\n"; } ?> </BODY> </HTML>