1XML::Feed(3) User Contributed Perl Documentation XML::Feed(3)
2
3
4
6 XML::Feed - Syndication feed parser and auto-discovery
7
9 use XML::Feed;
10 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
11 or die XML::Feed->errstr;
12 print $feed->title, "\n";
13 for my $entry ($feed->entries) {
14 }
15
16 ## Find all of the syndication feeds on a given page, using
17 ## auto-discovery.
18 my @feeds = XML::Feed->find_feeds('http://example.com/');
19
21 XML::Feed is a syndication feed parser for both RSS and Atom feeds. It
22 also implements feed auto-discovery for finding feeds, given a URI.
23
24 XML::Feed supports the following syndication feed formats:
25
26 · RSS 0.91
27
28 · RSS 1.0
29
30 · RSS 2.0
31
32 · Atom
33
34 The goal of XML::Feed is to provide a unified API for parsing and using
35 the various syndication formats. The different flavors of RSS and Atom
36 handle data in different ways: date handling; summaries and content;
37 escaping and quoting; etc. This module attempts to remove those
38 differences by providing a wrapper around the formats and the classes
39 implementing those formats (XML::RSS and XML::Atom::Feed). For example,
40 dates are handled differently in each of the above formats. To provide
41 a unified API for date handling, XML::Feed converts all date formats
42 transparently into DateTime objects, which it then returns to the
43 caller.
44
46 XML::Feed->new($format)
47 Creates a new empty XML::Feed object using the format $format.
48
49 $feed = XML::Feed->new('Atom');
50 $feed = XML::Feed->new('RSS');
51 $feed = XML::Feed->new('RSS', version => '0.91');
52
53 XML::Feed->parse($stream)
54 XML::Feed->parse($stream, $format)
55 Parses a syndication feed identified by $stream and returns an
56 XML::Feed object. $stream can be any one of the following:
57
58 · Scalar reference
59
60 A reference to string containing the XML body of the feed.
61
62 · Filehandle
63
64 An open filehandle from which the feed XML will be read.
65
66 · File name
67
68 The name of a file containing the feed XML.
69
70 · URI object
71
72 A URI from which the feed XML will be retrieved.
73
74 $format allows you to override format guessing.
75
76 XML::Feed->get_uri($uri)
77 Gets a feed from a URI.
78
79 XML::Feed->find_feeds($uri)
80 Given a URI $uri, use auto-discovery to find all of the feeds linked
81 from that page (using <link> tags).
82
83 Returns a list of feed URIs.
84
85 XML::Feed->identify_format(\$xml)
86 Given the xml of a feed return what format it is in, with "Atom" or
87 "RSS" for all versions of RSS. Note that you pass in a scalar ref to
88 the xml string.
89
90 $feed->convert($format)
91 Converts the XML::Feed object into the $format format, and returns the
92 new object.
93
94 $feed->splice($other_feed)
95 Splices in all of the entries from the feed $other_feed into $feed,
96 skipping posts that are already in $feed.
97
98 $feed->format
99 Returns the format of the feed ("Atom", or some version of "RSS").
100
101 $feed->title([ $title ])
102 The title of the feed/channel.
103
104 $feed->base([ $base ])
105 The url base of the feed/channel.
106
107 $feed->link([ $uri ])
108 The permalink of the feed/channel.
109
110 $feed->tagline([ $tagline ])
111 The description or tagline of the feed/channel.
112
113 $feed->description([ $description ])
114 Alias for $feed->tagline.
115
116 $feed->author([ $author ])
117 The author of the feed/channel.
118
119 $feed->language([ $language ])
120 The language of the feed.
121
122 $feed->copyright([ $copyright ])
123 The copyright notice of the feed.
124
125 $feed->modified([ $modified ])
126 A DateTime object representing the last-modified date of the feed.
127
128 If present, $modified should be a DateTime object.
129
130 $feed->generator([ $generator ])
131 The generator of the feed.
132
133 $feed->self_link ([ $uri ])
134 The Atom Self-link of the feed:
135
136 <http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
137
138 A string.
139
140 $feed->entries
141 A list of the entries/items in the feed. Returns an array containing
142 XML::Feed::Entry objects.
143
144 $feed->items
145 A synonym (alias) for <$feed->entries>.
146
147 $feed->add_entry($entry)
148 Adds an entry to the feed. $entry should be an XML::Feed::Entry object
149 in the correct format for the feed.
150
151 $feed->as_xml
152 Returns an XML representation of the feed, in the format determined by
153 the current format of the $feed object.
154
155 $feed->first_link ([ $uri ])
156 The Atom First-link for feed paging and archiving (RFC 5005).
157
158 <http://tools.ietf.org/html/rfc5005>
159
160 $feed->last_link ([ $uri ])
161 The Atom Last-link for feed paging and archiving.
162
163 $feed->next_link ([ $uri ])
164 The Atom Next-link for feed paging and archiving.
165
166 $feed->previous_link ([ $uri ])
167 The Atom Previous-link for feed paging and archiving.
168
169 $feed->current_link ([ $uri ])
170 The Atom Current-link for feed paging and archiving.
171
172 $feed->next_archive_link ([ $uri ])
173 The Atom Next-link for feed paging and archiving.
174
175 $feed->prev_archive_link ([ $uri ])
176 The Atom Prev-Archive-link for feed paging and archiving.
177
179 $XML::Feed::Format::RSS::PREFERRED_PARSER
180 If you want to use another RSS parser class than XML::RSS
181 (default), you can change the class by setting $PREFERRED_PARSER
182 variable in the XML::Feed::Format::RSS package.
183
184 $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
185
186 Note: this will only work for parsing feeds, not creating feeds.
187
188 Note: Only "XML::RSS::LibXML" version 0.3004 is known to work at
189 the moment.
190
191 $XML::Feed::MULTIPLE_ENCLOSURES
192 Although the RSS specification states that there can be at most one
193 enclosure per item some feeds break this rule.
194
195 If this variable is set then "XML::Feed" captures all of them and
196 makes them available as a list.
197
198 Otherwise it returns the last enclosure parsed.
199
200 Note: "XML::RSS" version 1.44 is needed for this to work.
201
203 For reference, this cgi script will create valid, albeit nonsensical
204 feeds (according to "http://feedvalidator.org" anyway) for Atom 1.0 and
205 RSS 0.90, 0.91, 1.0 and 2.0.
206
207 #!perl -w
208
209 use strict;
210 use CGI;
211 use CGI::Carp qw(fatalsToBrowser);
212 use DateTime;
213 use XML::Feed;
214
215 my $cgi = CGI->new;
216 my @args = ( $cgi->param('format') // "Atom" );
217 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
218
219 my $feed = XML::Feed->new(@args);
220 $feed->id("http://".time.rand()."/");
221 $feed->title('Test Feed');
222 $feed->link($cgi->url);
223 $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
224 $feed->modified(DateTime->now);
225
226 my $entry = XML::Feed::Entry->new();
227 $entry->id("http://".time.rand()."/");
228 $entry->link("http://example.com");
229 $entry->title("Test entry");
230 $entry->summary("Test summary");
231 $entry->content("Foo");
232 $entry->modified(DateTime->now);
233 $entry->author('test@example.com (Testy McTesterson)');
234 $feed->add_entry($entry);
235
236 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
237 print $cgi->header($mime);
238 print $feed->as_xml;
239
241 XML::Feed is free software; you may redistribute it and/or modify it
242 under the same terms as Perl itself.
243
245 Except where otherwise noted, XML::Feed is Copyright 2004-2008 Six
246 Apart. All rights reserved.
247
249 For support contact the XML::Feed mailing list -
250 xml-feed@perlhacks.com.
251
253 The latest version of XML::Feed can be found at
254
255 http://github.com/davorg/XML-Feed
256
257
258
259perl v5.30.1 2020-01-30 XML::Feed(3)