RSS to an HTML List


Here's a quick snippet for outputting an RSS Feed to an html list.

It's nothing fancy - I put it together in a few minutes, but I figured I'd save you a little bit of trouble in figuring it out all by yourself.

It uses Perl, with the modules XML::RSS and LWP::Simple. I tested this out with RSS 2.0 feeds and it works fine. I know that XML::RSS can handle RSS 1.0 and RSS 0.9 feeds, but I don't know if it auto-detects the feed version or not. You may have to make a minor edit to the script in order to get legacy RSS support.

collapsehide line numbers
Sample Code
  1<br />
  2#!/usr/bin/perl -t<br />
  3use strict;<br />
  4use warnings;<br />
  5use XML::RSS;<br />
  6use LWP::Simple;<br />
  7my $rss = new XML::RSS;<br />
  8my $outfile = '/where/you/want/output';<br />
  9my $title;<br />
 10my $out = '<ul>';<br />
 11my $rssfile = get('http://address.of.feed/rssfile.rss');<br />
 12#print $rssfile;<br />
 13$rss->parsestring($rssfile);<br />
 14foreach my $item (@{$rss->{'items'}}) {<br />
 15$title = $item->{'title'};<br />
 16$title =~ s/"/\\"/g;<br />
 17$out .= "<li><a href=\"$item->{'link'}\" title=\"$item->{'title'}\">$item->{'title'}</a></li>";<br />
 18}<br />
 19$out .= '</ul>';<br />
 20open(RSS,">$outfile")||die("$outfile cannot be opened");<br />
 21print RSS $out;<br />
 22close(RSS);<br />
Code ©SteveKallestad.com

You can run the script directly from the command line or incorporate it pretty easily into anything else. You could certainly do a lot more with XML::RSS, and you may want to implement some sort of caching so that you aren't downloading full feeds repeatedly, but in my particular situation, this script works great all by itself.

Line numbers 7 and 11 are the ones you want to edit.



RSS to an HTML List Feedback