perl XML::RSS创建RSS文件并解析
参考文献:http://search.cpan.org/~shlomif/XML-RSS-1.49/lib/XML/RSS.pm脚本:
# cat rss1.pl
#!/usr/bin/perl -w
use strict;
use XML::RSS;
my $rss = XML::RSS->new;
$rss->channel(title => 'Dave News',
link => 'http://daves.news',
description => "All the news that's unfit to print!",
dc => {
date => scalar localtime,
publisher => 'ed@daves.news',
language => 'en',
rights => 'Copyright 1999, USA, Larry Wall.',
},
);
$rss->image(title => "Dave's News",
url => 'http://daves.news/images/logo.gif',
link => 'http://daves.news');
$rss->add_item(title => 'Data Munging Book tops best sellers list',
link => 'http://daves.news/cgi-bin/read.pl?id=1');
$rss->add_item(title => 'Microsoft abandons ASP for Perl',
link => 'http://daves.news/cgi-bin/read.pl?id=2');
$rss->add_item(title => 'Gates offers job to Torvalds',
link => 'http://daves.news/cgi-bin/read.pl?id=3');
$rss->save('news.rss');
执行脚本:
# perl rss1.pl
# cat news.rss
Dave News
http://daves.news
All the news that's unfit to print!
en
Copyright 1999, USA, Larry Wall.
Tue Nov 27 01:10:50 2012
ed@daves.news
Dave's News
http://daves.news/images/logo.gif
http://daves.news
Data Munging Book tops best sellers list
http://daves.news/cgi-bin/read.pl?id=1
Microsoft abandons ASP for Perl
http://daves.news/cgi-bin/read.pl?id=2
Gates offers job to Torvalds
http://daves.news/cgi-bin/read.pl?id=3
#
解析脚本:
# cat parsernew.pl
#!/usr/bin/perl -w
use strict;
use XML::RSS;
my $file = "news.rss";
my $rss = XML::RSS->new;
$rss->parsefile($file);
print $rss->channel('title'),"\n";
print $rss->channel('description'),"\n";
print $rss->channel('link'),"\n";
print 'Published: ',$rss->channel('dc')->{publisher},"\n";
print "Items:\n";
foreach my $item (@{$rss->{'items'}}) {
print $item->{title},"\n\t{link},">\n";
}
执行结果:
# perl parsernew.pl
Dave News
All the news that's unfit to print!
http://daves.news
Published: ed@daves.news
Items:
Data Munging Book tops best sellers list
Microsoft abandons ASP for Perl
Gates offers job to Torvalds
#
页:
[1]