Parse/write/merge/edit rss/rdf/atom syndication feeds
Get an \s-1RSS\s0 file and parse it:
my $source = 'http://use.perl.org/index.rss'; my $feed = XML::FeedPP->new( $source ); print "Title: ", $feed->title(), "\n"; print "Date: ", $feed->pubDate(), "\n"; foreach my $item ( $feed->get_item() ) { print "URL: ", $item->link(), "\n"; print "Title: ", $item->title(), "\n"; }
Generate an \s-1RDF\s0 file and save it:
my $feed = XML::FeedPP::RDF->new(); $feed->title( "use Perl" ); $feed->link( "http://use.perl.org/" ); $feed->pubDate( "Thu, 23 Feb 2006 14:43:43 +0900" ); my $item = $feed->add_item( "http://search.cpan.org/~kawasaki/XML-TreePP-0.02" ); $item->title( "Pure Perl implementation for parsing/writing xml file" ); $item->pubDate( "2006-02-23T14:43:43+09:00" ); $feed->to_file( "index.rdf" );
Convert some \s-1RSS/RDF\s0 files to Atom format:
my $feed = XML::FeedPP::Atom::Atom10->new(); # create empty atom file $feed->merge( "rss.xml" ); # load local RSS file $feed->merge( "http://www.kawa.net/index.rdf" ); # load remote RDF file my $now = time(); $feed->pubDate( $now ); # touch date my $atom = $feed->to_string(); # get Atom source code
\*(C`XML::FeedPP\*(C' is an all-purpose syndication utility that parses and publishes \s-1RSS\s0 2.0, \s-1RSS\s0 1.0 (\s-1RDF\s0), Atom 0.3 and 1.0 feeds. It allows you to add new content, merge feeds, and convert among these various formats. It is a pure Perl implementation and does not require any other module except for XML::TreePP.
This constructor method creates an \*(C`XML::FeedPP\*(C' feed instance. The only argument is the local filename. The format of $source must be one of the supported feed formats \*(-- \s-1RSS\s0, \s-1RDF\s0 or Atom \*(-- or execution is halted. The \s-1URL\s0 on the remote web server is also available as the first argument. LWP::UserAgent is required to download it. The \s-1XML\s0 source code is also available as the first argument. The \*(C`-type\*(C' argument allows you to specify type of $source from choice of 'file', 'url' or 'string'. This makes utf8 flag on for every feed elements. Perl 5.8.1 or later is required to use this.
Note that any other options for \*(C`XML::TreePP\*(C' constructor are also allowed like this. See more detail on XML::TreePP. This constructor method creates an instance for an \s-1RSS\s0 2.0 feed. The first argument is optional, but must be valid an \s-1RSS\s0 source if specified. This method returns an empty instance when $source is undefined. This constructor method creates an instance for \s-1RSS\s0 1.0 (\s-1RDF\s0) feed. The first argument is optional, but must be an \s-1RDF\s0 source if specified. This method returns an empty instance when $source is undefined. This constructor method creates an instance for an Atom 0.3/1.0 feed. The first argument is optional, but must be an Atom source if specified. This method returns an empty instance when $source is undefined.
Atom 1.0 feed is also supported since \*(C`XML::FeedPP\*(C' version 0.30. Atom 0.3 is still default, however, future version of this module would create Atom 1.0 as default. This creates an empty Atom 0.3 instance obviously. This creates an empty Atom 1.0 instance intended. This creates a \s-1RSS\s0 instance which has \*(C`link\*(C', \*(C`title\*(C' elements etc. This method loads an RSS/RDF/Atom file, much like \*(C`new()\*(C' method does. This method merges an RSS/RDF/Atom file into the existing $feed instance. Top-level metadata from the imported feed is incorporated only if missing from the present feed. This method generates \s-1XML\s0 source as string and returns it. The output $encoding is optional, and the default encoding is '\s-1UTF-8\s0'. On Perl 5.8 and later, any encodings supported by the Encode module are available. On Perl 5.005 and 5.6.1, only four encodings supported by the Jcode module are available: '\s-1UTF-8\s0', 'Shift_JIS', '\s-1EUC-JP\s0' and '\s-1ISO-2022-JP\s0'. '\s-1UTF-8\s0' is recommended for overall compatibility. This makes the output more human readable by indenting appropriately. This does not strictly follow the \s-1XML\s0 specification but does looks nice.
Note that any other options for \*(C`XML::TreePP\*(C' constructor are also allowed like this. See more detail on XML::TreePP. This method generate an \s-1XML\s0 file. The output $encoding is optional, and the default is '\s-1UTF-8\s0'. This method creates a new item/entry and returns its instance. A mandatory $link argument is the \s-1URL\s0 of the new item/entry. This method duplicates an item/entry and adds it to $feed. $srcitem is a \*(C`XML::FeedPP::*::Item\*(C' class's instance which is returned by \*(C`get_item()\*(C' method, as described above. This method creates an new item/entry which has \*(C`link\*(C', \*(C`title\*(C' elements etc. This method returns item(s) in a $feed. A valid zero-based array $index returns the corresponding item in the feed. An invalid $index yields undef. If $index is undefined in array context, it returns an array of all items. If $index is undefined in scalar context, it returns the number of items. This method finds item(s) which match all regular expressions given. This method returns an array of all matched items in array context. This method returns the first matched item in scalar context. This method removes an item/entry specified by zero-based array index or link \s-1URL\s0. This method removes all items/entries from the $feed. This method sorts the order of items in $feed by \*(C`pubDate\*(C'. This method makes items unique. The second and succeeding items that have the same link \s-1URL\s0 are removed. This method calls both the \*(C`sort_item()\*(C' and \*(C`uniq_item()\*(C' methods. Removes items in excess of the specified numeric limit. Items at the end of the list are removed. When preceded by \*(C`sort_item()\*(C' or \*(C`normalize()\*(C', this deletes more recent items. Adds an \s-1XML\s0 namespace at the document root of the feed. Returns the \s-1URL\s0 of the specified \s-1XML\s0 namespace. Returns the list of all \s-1XML\s0 namespaces used in $feed.
This method sets/gets the feed's \*(C`title\*(C' element, returning its current value when $title is undefined. This method sets/gets the feed's \*(C`description\*(C' element in plain text or \s-1HTML\s0, returning its current value when $html is undefined. It is mapped to \*(C`content\*(C' element for Atom 0.3/1.0. This method sets/gets the feed's \*(C`pubDate\*(C' element for \s-1RSS\s0, returning its current value when $date is undefined. It is mapped to \*(C`dc:date\*(C' element for \s-1RDF\s0, \*(C`modified\*(C' for Atom 0.3, and \*(C`updated\*(C' for Atom 1.0. See also \*(L"\s-1DATE\s0 \s-1AND\s0 \s-1TIME\s0 \s-1FORMATS\s0\*(R" section below. This method sets/gets the feed's \*(C`copyright\*(C' element for \s-1RSS\s0, returning its current value when $text is undefined. It is mapped to \*(C`dc:rights\*(C' element for \s-1RDF\s0, \*(C`copyright\*(C' for Atom 0.3, and \*(C`rights\*(C' for Atom 1.0. This method sets/gets the \s-1URL\s0 of the web site as the feed's \*(C`link\*(C' element, returning its current value when the $url is undefined. This method sets/gets the feed's \*(C`language\*(C' element for \s-1RSS\s0, returning its current value when the $lang is undefined. It is mapped to \*(C`dc:language\*(C' element for \s-1RDF\s0, \*(C`feed xml:lang=""\*(C' for Atom 0.3/1.0. This method sets/gets the feed's \*(C`image\*(C' element and its child nodes, returning a list of current values when any arguments are undefined.
This method sets/gets the item's \*(C`title\*(C' element, returning its current value when the $text is undefined. This method sets/gets the item's \*(C`description\*(C' element in \s-1HTML\s0 or plain text, returning its current value when $text is undefined. It is mapped to \*(C`content\*(C' element for Atom 0.3/1.0. This method sets/gets the item's \*(C`pubDate\*(C' element, returning its current value when $date is undefined. It is mapped to \*(C`dc:date\*(C' element for \s-1RDF\s0, \*(C`modified\*(C' for Atom 0.3, and \*(C`updated\*(C' for Atom 1.0. See also \*(L"\s-1DATE\s0 \s-1AND\s0 \s-1TIME\s0 \s-1FORMATS\s0\*(R" section below. This method sets/gets the item's \*(C`category\*(C' element. returning its current value when $text is undefined. It is mapped to \*(C`dc:subject\*(C' element for \s-1RDF\s0, and ignored for Atom 0.3. This method sets/gets the item's \*(C`author\*(C' element, returning its current value when $name is undefined. It is mapped to \*(C`dc:creator\*(C' element for \s-1RDF\s0, \*(C`author\*(C' for Atom 0.3/1.0. This method sets/gets the item's \*(C`guid\*(C' element, returning its current value when $guid is undefined. It is mapped to \*(C`id\*(C' element for Atom, and ignored for \s-1RDF\s0. The second argument is optional. This method sets customized node values or attributes. See also \*(L"\s-1ACCESSOR\s0 \s-1AND\s0 \s-1MUTATORS\s0\*(R" section below. This method returns the node value or attribute. See also \*(L"\s-1ACCESSOR\s0 \s-1AND\s0 \s-1MUTATORS\s0\*(R" section below. This method returns the item's \*(C`link\*(C' element.
This module understands only subset of \*(C`rdf:*\*(C', \*(C`dc:*\*(C' modules and RSS/RDF/Atom's default namespaces by itself. There are \s-1NO\s0 native methods for any other external modules, such as \*(C`media:*\*(C'. But \*(C`set()\*(C' and \*(C`get()\*(C' methods are available to get/set the value of any elements or attributes for these modules. This sets the value of the child node:
<item><module:name>$value</module:name>...</item> This sets the value of the child node's attribute:
<item><module:name attr="$value" />...</item> This sets the value of the item's attribute:
<item attr="$value">...</item> This code sets the value of the child node's child node's attribute:
<item><hoge><pomu attr="$value" /></hoge>...</item>
\*(C`XML::FeedPP\*(C' allows you to describe date/time using any of the three following formats: This is the \s-1HTTP\s0 protocol's preferred format and \s-1RSS\s0 2.0's native format, as defined by \s-1RFC\s0 1123. W3CDTF is the native format of \s-1RDF\s0, as defined by \s-1ISO\s0 8601. The last format is the number of seconds since the epoch, \*(C`1970-01-01T00:00:00Z\*(C'. You know, this is the native format of Perl's \*(C`time()\*(C' function.
To publish Media \s-1RSS\s0, add the \*(C`media\*(C' namespace then use \*(C`set()\*(C' setter method to manipulate \*(C`media:content\*(C' element, etc.
my $feed = XML::FeedPP::RSS->new(); $feed->xmlns('xmlns:media' => 'http://search.yahoo.com/mrss/'); my $item = $feed->add_item('http://www.example.com/index.html'); $item->set('media:content@url' => 'http://www.example.com/image.jpg'); $item->set('media:content@type' => 'image/jpeg'); $item->set('media:content@width' => 640); $item->set('media:content@height' => 480);
\*(C`XML::FeedPP\*(C' requires only XML::TreePP which likewise is a pure Perl implementation. The standard LWP::UserAgent is required to download feeds from remote web servers. \*(C`Jcode.pm\*(C' is required to convert Japanese encodings on Perl 5.005 and 5.6.1, but is \s-1NOT\s0 required on Perl 5.8.x and later.
Yusuke Kawasaki, http://www.kawa.net/
The following copyright notice applies to all the files provided in this distribution, including binary files, unless explicitly noted otherwise.
Copyright 2006-2011 Yusuke Kawasaki
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.