Syndication feed parser and auto-discovery
use XML::Feed; my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml')) or die XML::Feed->errstr; print $feed->title, "\n"; for my $entry ($feed->entries) { } ## Find all of the syndication feeds on a given page, using ## auto-discovery. my @feeds = XML::Feed->find_feeds('http://example.com/');
XML::Feed is a syndication feed parser for both \s-1RSS\s0 and Atom feeds. It also implements feed auto-discovery for finding feeds, given a \s-1URI\s0.
XML::Feed supports the following syndication feed formats:
\s-1RSS\s0 0.91
\s-1RSS\s0 1.0
\s-1RSS\s0 2.0
Atom
The goal of XML::Feed is to provide a unified \s-1API\s0 for parsing and using the various syndication formats. The different flavors of \s-1RSS\s0 and Atom handle data in different ways: date handling; summaries and content; escaping and quoting; etc. This module attempts to remove those differences by providing a wrapper around the formats and the classes implementing those formats (\s-1XML::RSS\s0 and XML::Atom::Feed). For example, dates are handled differently in each of the above formats. To provide a unified \s-1API\s0 for date handling, XML::Feed converts all date formats transparently into DateTime objects, which it then returns to the caller.
Creates a new empty XML::Feed object using the format $format.
$feed = XML::Feed->new('Atom'); $feed = XML::Feed->new('RSS'); $feed = XML::Feed->new('RSS', version => '0.91');
Parses a syndication feed identified by $stream and returns an XML::Feed obhect. $stream can be any one of the following:
Scalar reference A reference to string containing the \s-1XML\s0 body of the feed.
Filehandle An open filehandle from which the feed \s-1XML\s0 will be read.
File name The name of a file containing the feed \s-1XML\s0.
\s-1URI\s0 object A \s-1URI\s0 from which the feed \s-1XML\s0 will be retrieved.
$format allows you to override format guessing.
Given a \s-1URI\s0 $uri, use auto-discovery to find all of the feeds linked from that page (using <link> tags).
Returns a list of feed URIs.
Given the xml of a feed return what format it is in (\*(C`Atom\*(C', or some version of \*(C`RSS\*(C'). Converts the XML::Feed object into the $format format, and returns the new object. Splices in all of the entries from the feed $other_feed into $feed, skipping posts that are already in $feed. Returns the format of the feed (\*(C`Atom\*(C', or some version of \*(C`RSS\*(C'). The title of the feed/channel. The url base of the feed/channel. The permalink of the feed/channel. The description or tagline of the feed/channel. Alias for $feed->tagline. The author of the feed/channel. The language of the feed. The copyright notice of the feed. A DateTime object representing the last-modified date of the feed.
If present, $modified should be a DateTime object. The generator of the feed. The Atom Self-link of the feed:
<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
A string. A list of the entries/items in the feed. Returns an array containing XML::Feed::Entry objects. A synonym (alias) for <$feed->entries>. Adds an entry to the feed. $entry should be an XML::Feed::Entry object in the correct format for the feed. Returns an \s-1XML\s0 representation of the feed, in the format determined by the current format of the $feed object. The Atom First-link for feed paging and archiving (\s-1RFC\s0 5005).
<http://tools.ietf.org/html/rfc5005> The Atom Last-link for feed paging and archiving. The Atom Next-link for feed paging and archiving. The Atom Previous-link for feed paging and archiving. The Atom Current-link for feed paging and archiving. The Atom Next-link for feed paging and archiving. The Atom Prev-Archive-link for feed paging and archiving.
If you want to use another \s-1RSS\s0 parser class than \s-1XML::RSS\s0 (default), you can change the class by setting $PREFERRED_PARSER variable in the XML::Feed::Format::RSS package. $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML"; Note: this will only work for parsing feeds, not creating feeds. Note: Only \*(C`XML::RSS::LibXML\*(C' version 0.3004 is known to work at the moment. Although the \s-1RSS\s0 specification states that there can be at most one enclosure per item some feeds break this rule. If this variable is set then \*(C`XML::Feed\*(C' captures all of them and makes them available as a list. Otherwise it returns the last enclosure parsed. Note: \*(C`XML::RSS\*(C' version 1.44 is needed for this to work.
For reference, this cgi script will create valid, albeit nonsensical feeds (according to \*(C`http://feedvalidator.org\*(C' anyway) for Atom 1.0 and \s-1RSS\s0 0.90, 0.91, 1.0 and 2.0.
#!perl -w
use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use DateTime; use XML::Feed;
my $cgi = CGI->new; my @args = ( $cgi->param('format') || "Atom" ); push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
my $feed = XML::Feed->new(@args); $feed->id("http://".time.rand()."/"); $feed->title('Test Feed'); $feed->link($cgi->url); $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) ); $feed->modified(DateTime->now);
my $entry = XML::Feed::Entry->new(); $entry->id("http://".time.rand()."/"); $entry->link("http://example.com"); $entry->title("Test entry"); $entry->summary("Test summary"); $entry->content("Foo"); $entry->modified(DateTime->now); $entry->author('[email protected] (Testy McTesterson)'); $feed->add_entry($entry);
my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml"; print $cgi->header($mime); print $feed->as_xml;
XML::Feed is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
Except where otherwise noted, XML::Feed is Copyright 2004-2008 Six Apart. All rights reserved.
For support contact the XML::Feed mailing list - [email protected].
The latest version of XML::Feed can be found at
http://github.com/davorg/XML-Feed