Saturday, February 26, 2005

XML::Parser of RSS

I have drunk the XML Kool-aid, also the Perl References Kool-aid.

#!perl -wl
use XML::Parser;
use LWP::Simple;
$content=get("http://earthquake.usgs.gov/recenteqsww/catalogs/eqs1day-M2.5.xml");
my $xml=new XML::Parser(Style => 'Tree');
$f=$xml->parse($content);
exit unless $$f[0] eq 'rss';
@g=&findelement('channel',@$f[1]);
@items=&findelement('item',$g[0]);
for (@items) {
  @s=&findelement('dc:subject',$_);
  die unless @s==2;
  $mag=&intext($s[0]);
  $post=&intext($s[1]);
  $title=&gettext('title',$_);
  $link=&gettext('link',$_);
  $date=&gettext('description',$_);
  print "$title\n$date\n$link\n$mag $post\n";
}

sub findelement { my ($tag,$val); my @ret; my ($target,$arr)=@_; @_=@$arr; shift; #throw away attribute hash while (@_) { $tag=shift; $val=shift; push @ret,$val if ($tag eq $target); } @ret; }

sub gettext { my ($target,$arr)=@_; my @t0=&findelement($target,$arr); &intext(@t0); }

sub intext { my @t1=&findelement('0',$_[0]); $t1[0]; }

No comments :