参考文献:http://search.cpan.org/~shlomif/XML-RSS-1.49/lib/XML/RSS.pm
脚本:
[root@dou xml]# 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');
执行脚本:
[root@dou xml]# perl RSS1.pl
[root@dou xml]# cat news.RSS
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
>
<channel rdf:about="http://daves.news">
<title>Dave News</title>
<link>http://daves.news</link>
<description>All the news that's unfit to print!</description>
<dc:language>en</dc:language>
<dc:rights>copyright 1999,Larry Wall.</dc:rights>
<dc:date>Tue Nov 27 01:10:50 2012</dc:date>
<dc:publisher>ed@daves.news</dc:publisher>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://daves.news/cgi-bin/read.pl?id=1" />
<rdf:li rdf:resource="http://daves.news/cgi-bin/read.pl?id=2" />
<rdf:li rdf:resource="http://daves.news/cgi-bin/read.pl?id=3" />
</rdf:Seq>
</items>
<image rdf:resource="http://daves.news/images/logo.gif" />
</channel>
<image rdf:about="http://daves.news/images/logo.gif">
<title>Dave's News</title>
<url>http://daves.news/images/logo.gif</url>
<link>http://daves.news</link>
</image>
<item rdf:about="http://daves.news/cgi-bin/read.pl?id=1">
<title>Data Munging Book tops best sellers list</title>
<link>http://daves.news/cgi-bin/read.pl?id=1</link>
</item>
<item rdf:about="http://daves.news/cgi-bin/read.pl?id=2">
<title>Microsoft abandons ASP for Perl</title>
<link>http://daves.news/cgi-bin/read.pl?id=2</link>
</item>
<item rdf:about="http://daves.news/cgi-bin/read.pl?id=3">
<title>Gates offers job to Torvalds</title>
<link>http://daves.news/cgi-bin/read.pl?id=3</link>
</item>
</rdf:RDF>
[root@dou xml]#
解析脚本:
[root@dou xml]# 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<",$item->{link},">\n";
}
执行结果:
[root@dou xml]# 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
<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> [root@dou xml]#