加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl XML::RSS创建RSS文件并解析

发布时间:2020-12-15 21:01:13 所属栏目:大数据 来源:网络整理
导读:参考文献: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', des

参考文献: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');

$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="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&#x27;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&#x27;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},"nt<",$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]# ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读