Computing.Net > Forums > Web Development > RSS parser code problem in PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

RSS parser code problem in PHP

Reply to Message Icon

Name: Alirezan1
Date: August 5, 2004 at 00:33:14 Pacific
OS: Linux/ XP
CPU/Ram: AMD
Comment:

Hello everyone,

I have been trying to write a simple code that reads and RSS feed from yahoo! website and parses it and writes the contents on a new page. However, I have this problem that when I tried to use a separate function to format the output HTML, everything got distorted. The code below works, but if you try to use the other option (the function) and uncomment the commented lines and use them, everything goes wrong. Please tell me what is wrong with my code? Thanks

<?php
$open_stack = array();
$parser = xml_parser_create();

xml_set_element_handler ( $parser, "start_handler", "end_handler" );
xml_set_character_data_handler ( $parser, "character_handler" );
xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, 0 );

print "<html><title>ARNIT NEWS CENTRE</head><body>\n\n";

xml_parse ($parser, file_get_contents ( "http://rss.news.yahoo.com/rss/tech" ) )
or die ( format_error ( $parser ) );

xml_parser_free ( $parser );
print "</body></html>";


function start_handler ( $p, $name, $atts ) {
global $open_stack;
$open_stack[] = array($name, "");
}

function character_handler ( $p, $txt ) {
global $open_stack;
$cur_index = count($open_stack)-1;

$open_stack [$cur_index][1] =
$open_stack[$cur_index][1].$txt;

}

function end_handler ( $p, $name ) {
global $open_stack;
$el = array_pop ( $open_stack );

switch ( $name )
{
case 'title':
//$title = $el[1];
print "$el[1]";
break;

case 'description':
//$description = $el[1];

print "$d$el[1]

";
break;

case 'link':
//$links = $el[1];
print "
Read more\n\n\n
";
break;

}
//format_html ( $title, $details, $links );
}


function format_html ( $t, $d, $l ) {

print "$t";
print "$d\n

";
print "
Read more\n\n\n
";
}


function format_error ( $p ) {

$code = xml_get_error_code ( $p );
$str = xml_error_string ( $code );
$line = xml_get_current_line_number ( $p );
return "XML ERROR ($code): $str at line $line";
}
?>



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


sql server How should I proceed with...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: RSS parser code problem in PHP

String replacement problem in PHP www.computing.net/answers/webdevel/string-replacement-problem-in-php/675.html

mysql_numrows() error in php www.computing.net/answers/webdevel/mysqlnumrows-error-in-php/770.html

error in php www.computing.net/answers/webdevel/error-in-php/3595.html