12-31-04, 12:42 PM
Newbie Coder
Join Date: Mar 2004
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Restricting the results my xml feed returns
I have got a bit of code which returns all of the headlines inside the newsfeed stated, but as this takes up quite a bit of space I want to limit the headlines it returns to the first 10.
The current demo which returns them all is at
http://www.blueuniverse.co.uk/news.php
The code I am using is
PHP Code:
<? $xml_file = "http://p.moreover.com/cgi-local/page?c=3M%20news&o=xml" ; //change this to whatever news feed you want to use $open_tags = array( "ARTICLE" => "<ARTICLE>" , "HEADLINE_TEXT" => "<HEADLINE_TEXT>" , "URL" => "<URL>" , "HARVEST_TIME" => "<HARVEST_TIME>" , "SOURCE" => "<SOURCE>" ); $close_tags = array( "ARTICLE" => "</ARTICLE>" , "HEADLINE_TEXT" => "</HEADLINE_TEXT>" , "HARVEST_TIME" => "<HARVEST_TIME>" , "URL" => "</URL>" , "SOURCE" => "</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 "<a href=\"" . $temp [ "url" ]. "\" target=\"_blank\">" . $temp [ "head" ]. "</a>\n<br />\n" ; echo "[" . $temp [ "source" ]. "] - " . $temp [ "time" ]. "\n<br />\n<br />\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 ); ?>
Could anybody tell me what I would have to change this to?