在 Perl 中解析 RSS/Atom 的最佳库是什么?

发布于 2024-09-27 20:53:30 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少女净妖师 2024-10-04 20:53:30

我不确定它是否曾经是“推荐的库”。如果我知道需要解析哪种类型的提要,我会使用 XML::RSSXML::Atom (视情况而定),但如果(更有可能)我只是知道这是一个网络提要,我使用 XML::Feed

根据要求添加使用 XML::Feed 的示例。

use XML::Feed;

my $feed = XML::Feed->parse(\$string_containing_feed);

foreach ($feed->entries) {
  print $_->title, "\n";
  print $_->content->body, "\n";
}

这几乎都是从模块文档中复制的。

I'm not sure it's ever been the "recommended library". If I know which kind of feed I need to parse, I use XML::RSS or XML::Atom as appropriate, but if (as is more likely) I just know it's a web feed, I use XML::Feed.

Adding an example of using XML::Feed as requested..

use XML::Feed;

my $feed = XML::Feed->parse(\$string_containing_feed);

foreach ($feed->entries) {
  print $_->title, "\n";
  print $_->content->body, "\n";
}

This is all pretty much copied from the module documentation.

萌吟 2024-10-04 20:53:30

实际上,现在我喜欢避免使用特定于域的 XML 解析器,而只使用 XPath 来处理所有事情。这样我只需要记住一个 API 即可。 (除非它是一个巨大的 XML,否则我将使用基于事件的解析器,例如 XML::Parser。 )

因此,使用 XML::XPath,我可以从 RSS 文件中获取一堆内容,如下所示

my $rss = get_rss();
my $xp = XML::XPath->new( xml => $rss );

my $stories = $xp->find( '/rss/channel/item' );

foreach my $story( $stories->get_nodelist ) {
    my $url   = $xp->find( 'link',  $story )->string_value;
    my $title = $xp->find( 'title', $story )->string_value;
    ...
}

:世界上最漂亮的代码,但它确实有效。

I actually like to avoid domain-specific XML parsers these days and just use XPath for everything. That way I only have to remember one API. (Unless it's a huge XML, then I'll use an event-based parser like XML::Parser.)

So using XML::XPath, I can grab a bunch of stuff from an RSS file like this:

my $rss = get_rss();
my $xp = XML::XPath->new( xml => $rss );

my $stories = $xp->find( '/rss/channel/item' );

foreach my $story( $stories->get_nodelist ) {
    my $url   = $xp->find( 'link',  $story )->string_value;
    my $title = $xp->find( 'title', $story )->string_value;
    ...
}

Not the prettiest code in the world, but it works.

や莫失莫忘 2024-10-04 20:53:30

如果 XML::RSS::Parser 适合您,那么就使用它。我已经使用 XML::Parser 来处理 RSS,但我的要求很窄,并且 XML::Parser 已经安装了。

仅仅因为某些东西在几年内进行了更新并不意味着它不再起作用;而是意味着它不再有效。我认为各种 RSS/Atom 规范最近没有改变,所以解析器不需要改变。

If XML::RSS::Parser works for you then use it. I've used XML::Parser to deal with RSS but I had narrow requirements and XML::Parser was already installed.

Just because something has been updated in a few years doesn't mean that it doesn't work anymore; I don't think the various RSS/Atom specs have changed recently so there's no need for the parser to change.

茶底世界 2024-10-04 20:53:30

还有一个非常好的模块,名为 XML::FeedPP (请参阅 http://search.cpan.org/dist/XML-FeedPP/lib/XML/FeedPP.pm)。 FeedPP 速度没有那么快,但它几乎是用纯 Perl 编写的,并且具有极简的依赖性。

There is also a very nice module called XML::FeedPP (see http://search.cpan.org/dist/XML-FeedPP/lib/XML/FeedPP.pm). FeedPP is no so fast but it writen in almost pure Perl and has minimalistic dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文