News
$xml_file = "http://p.moreover.com/cgi-local/page?index_topstories+xml";//change this to whatever news feed you want to use
$open_tags = array(
"ARTICLE" => "",
"HEADLINE_TEXT" => "",
"URL" => "",
"HARVEST_TIME" => "",
"SOURCE" => ""); $close_tags = array(
"ARTICLE" => "",
"HEADLINE_TEXT" => "",
"HARVEST_TIME" => "",
"URL" => "",
"SOURCE" => ""); $count=0;
function startElement($parser, $name, $attrs=""){
global $open_tags, $temp, $current_tag;
$current_tag = $name;
if ($format = $open_tags[$name]){
switch($name){
default:
break;
}
}
} function endElement($parser, $name, $attrs=""){
global $close_tags, $temp, $current_tag, $count, $numstors;
if ($format = $close_tags[$name]){
switch($name){
case "ARTICLE":
return_page($temp);
$temp = "";
break;
default:
break;
}
}
} function characterData($parser, $data){
global $current_tag, $temp, $catID;
switch($current_tag){
case "HEADLINE_TEXT":
$temp["head"] = $data;
$current_tag = "";
break;
case "URL":
$temp["url"] = $data;
$current_tag = "";
break;
case "SOURCE":
$temp["source"] = $data;
$current_tag = "";
break;
case "HARVEST_TIME":
$temp["time"] = $data;
$current_tag = "";
break;
default:
break;
}
} function return_page(){
global $temp, $count;
$temp["time"] = preg_replace ("/AM/", " a.m.", $temp["time"]);
$temp["time"] = preg_replace ("/PM/", " p.m.", $temp["time"]);
echo "".$temp["head"]."\n \n";
echo "[".$temp["source"]."] - ".$temp["time"]."\n \n \n\n";
} $xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement","endElement");
xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($xml_file, "r"))) {
die("Could not open $xml_file for parsing!\n");
}
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo "ERROR"."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
} xml_parser_free($xml_parser);
?> |