http://qs321.pair.com?node_id=400769

DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

I am cultivating an interest in scraping RSS feeds and the following program is my first real attempt:

#!/usr/bin/perl -w use strict; use XML::Parser; use LWP::Simple; my $feed; open( FH, ">feed.xml") or die "Error: $!\n"; $feed = get("http://rss.news.yahoo.com/rss/science"); print FH $feed; my $parser = new XML::Parser ( Handlers => { Start => \&hdl_start, End => \&hdl_end, Char => \&hdl_char, } ); + $parser->parsefile("feed.xml"); sub hdl_start { my ($p, $ele, %attribs) = @_; $attribs{'string'} = ''; $feed = \%attribs; } sub hdl_end { + my ($p, $ele) = @_; display_feed($feed) if $ele eq 'title'; display_feed($feed) if $ele eq 'link'; } sub hdl_char { my ($p, $str) = @_; no strict 'refs'; $feed->{'string'} .= $str; } sub display_feed { my $attribs = shift; $attribs =~ s/\n//g; print "$attribs->{'string'}\n\n"; }


The last title tag doesn't generate a corresponding link and the following message appears once the feed is displayed:

no element found at line 594, column 12, byte 45056 at C:/Perl/site/lib/XML/Parser.pm line 185

Ideally, the program would display the feed in html formatted links with the title directly above them for easy navigation to the story in question.

I've used XML::RSS before but I decided to experiment with an additional module.

Thanks,
~Katie