Forum Moderators: coopster
[edited by: eelixduppy at 11:08 pm (utc) on Oct. 2, 2008]
[edit reason] removed URL [/edit]
xmlparser.php
<?
class xmlParser{
var $xml_obj = null;
var $output = array();
function xmlParser(){
$this->xml_obj = xml_parser_create();
xml_set_object($this->xml_obj,$this);
xml_set_character_data_handler($this->xml_obj, 'dataHandler');
xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
}
function parse($path){
if (!($fp = fopen($path, "r"))) {
die("Cannot open XML data file: $path");
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_obj, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_obj)),
xml_get_current_line_number($this->xml_obj)));
xml_parser_free($this->xml_obj);
}
}
return true;
}
function startHandler($parser, $name, $attribs){
$_content = array('name' => $name);
if(!empty($attribs))
$_content['attrs'] = $attribs;
array_push($this->output, $_content);
}
function dataHandler($parser, $data){
if(!empty($data)) {
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['content'] = $data;
}
}
function endHandler($parser, $name){
if(count($this->output) > 1) {
$_data = array_pop($this->output);
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['child'][] = $_data;
}
}
function GetNodeByPath($path,$tree = false) {
if ($tree) {
$tree_to_search = $tree;
}
else {
$tree_to_search = $this->output;
}
if ($path == "") {
return null;
}
$arrPath = explode('/',$path);
foreach($tree_to_search as $key => $val) {
if (gettype($val) == "array") {
$nodename = $val[name];
if ($nodename == $arrPath[0]) {
if (count($arrPath) == 1) {
return $val;
}
array_shift($arrPath);
$new_path = implode($arrPath,"/");
return $this->GetNodeByPath($new_path,$val[child]);
}
}
}
}
}
?>
weathershow.php
<html>
<head>
</head>
<body>
<?
include("class.xml.parser.php");
include("class.weather.php");
$timeout=3*60*60; // 3 hours
if (isset($_ENV["TEMP"]))
$cachedir=$_ENV["TEMP"];
else if (isset($_ENV["TMP"]))
$cachedir=$_ENV["TMP"];
else if (isset($_ENV["TMPDIR"]))
$cachedir=$_ENV["TMPDIR"];
else
$cachedir="/tmp";
$cachedir=str_replace('\\\\','/',$cachedir);
if (substr($cachedir,-1)!='/') $cachedir.='/';
$weather_chile = new weather("CIXX0020", 3600, "C", $cachedir);
$weather_chile->parsecached();
print "<h1>Various</h1>";
print "title: ".$weather_chile->forecast['TITLE']."<br>";
print "city: ".$weather_chile->forecast['CITY']."<br>";
print "sunrise: ".$weather_chile->forecast['SUNRISE']."<br>";
print "sunset: ".$weather_chile->forecast['SUNSET']."<br>";
print "yahoolink: ".$weather_chile->forecast['LINK']."<br>";
print "<hr>";
print "<h1>Actual Situation</h1>";
print "acttext: ".$weather_chile->forecast['CURRENT']['TEXT']."<br>";
print "acttemp: ".$weather_chile->forecast['CURRENT']['TEMP']."<br>";
print "acttime: ".$weather_chile->forecast['CURRENT']['DATE']."<br>";
print "actcode: ".$weather_chile->forecast['CURRENT']['CODE']."<br>";
print "image: <img src=http://us.i1.yimg.com/us.yimg.com/i/us/we/52/".$weather_chile->forecast['CURRENT']['CODE'].".gif>";
print "<hr>";
for ($day=0; isset($weather_chile->forecast[$day]); $day++) {
print "<h1>Forecast Day $day</h1>";
print "day: ".$weather_chile->forecast[$day]['DAY']."<br>";
print "date: ".$weather_chile->forecast[$day]['DATE']."<br>";
print "low °C: ".$weather_chile->forecast[$day]['LOW']."<br>";
print "high °C: ".$weather_chile->forecast[$day]['HIGH']."<br>";
print "text: ".$weather_chile->forecast[$day]['TEXT']."<br>";
print "imgcode: ".$weather_chile->forecast[$day]['CODE']."<br>";
print "image: <img src=http://us.i1.yimg.com/us.yimg.com/i/us/we/52/".$weather_chile->forecast[$day]['CODE'].".gif>";
print "<hr>";
}
?>
</body>
</html>
thanks for help