Hi All,
I have a script I use to parse rss/xml files. I need to extract a small amount of data from a large number of files, so I have tried to create a function that would allow me to pass the url of the xml file as an arg and return the data. My problem is when I use the php xml functions from within my new function they dont work.
Any advice is greatly appreciated.
$waterData = "";
$xml_file = "http://www.crh.noaa.gov/rss/ahps2/obs/hrnm7.rss";
$xml_desc_key = "*RSS*CHANNEL*ITEM*DESCRIPTION";
$river_array = array();
$strRiver = "";
$counter = 0;
class xml_river{
var $description;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_desc_key, $counter, $river_array, $strRiver;
$river_array[$counter] = new xml_river();
if($current_tag == $xml_desc_key){
$river_array[$counter]->description = $data;
$strRiver = $strRiver." ".$river_array[$counter]->description;
$counter++;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, 80000);
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
if(preg_match("/Flood /", $river_array[0]->description)){
$waterData = $river_array[0]->description." ".$river_array[8]->description." ".$river_array[13]->description;
//echo $water;
}
else{
$waterData = $river_array[0]->description;
}