Creating an RSS Reader: the Reader - The Meat of the Code (
Page 3 of 4 )
Now that we have created the required functions, let's continue with the meat of the code:
function parsefile($RSSfile){
// Create an xml parser
$xmlParser = xml_parser_create();
// Set up element handler
xml_set_element_handler( $xmlParser, "startTag", "endTag" );
// Set up character handler
xml_set_character_data_handler( $xmlParser, "TxtTag" );
// Open connection to RSS XML file for parsing.
$fp = fopen( $RSSfile,"r" )
or die( "Cannot read RSS data file." );
// Parse XML data from RSS file.
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
// Close file open handler
fclose( $fp );
// Free xml parser from memory
xml_parser_free( $xmlParser );
}
The above function calls both the startTag/endTag functions to loop through the XML file and displays the contents.