参考文献:

脚本:

[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 => ,
 language => 'en',
 rights => 'Copyright 1999, USA, Larry Wall.',
 },
);

$rss->p_w_picpath(title => "Dave's News",

url => 'http://daves.news/p_w_picpaths/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');

 

执行脚本:

[root@dou xml]# perl rss1.pl
[root@dou xml]# cat news.rss
<?xml version="1.0" encoding="UTF-8"?>

<rdf:RDF

 xmlns:rdf="#"
 xmlns=""
 xmlns:content=""
 xmlns:taxo=""
 xmlns:dc=""
 xmlns:syn=""
 xmlns:admin=""
>

<channel rdf:about="">

<title>Dave News</title>
<link>http://daves.news</link>
<description>All the news that&#x27;s unfit to print!</description>
<dc:language>en</dc:language>
<dc:rights>Copyright 1999, USA, 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="" />
  <rdf:li rdf:resource="" />
  <rdf:li rdf:resource="" />
 </rdf:Seq>
</items>
<p_w_picpath rdf:resource="" />
</channel>
<p_w_picpath rdf:about="">
<title>Dave&#x27;s News</title>
<url>http://daves.news/p_w_picpaths/logo.gif</url>
<link>http://daves.news</link>
</p_w_picpath>
<item rdf:about="">
<title>Data Munging Book tops best sellers list</title>
<link>http://daves.news/cgi-bin/read.pl?id=1</link>
</item>
<item rdf:about="">
<title>Microsoft abandons ASP for Perl</title>
<link>http://daves.news/cgi-bin/read.pl?id=2</link>
</item>
<item rdf:about="">
<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!
Published:
Items:
Data Munging Book tops best sellers list
        <>
Microsoft abandons ASP for Perl
        <>
Gates offers job to Torvalds
        <>
[root@dou xml]#