XML Tutorials

  Home arrow XML Tutorials arrow Creating RSS 2.0 Feeds

Creating RSS 2.0 Feeds
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1

    Table of Contents:
  • Creating RSS 2.0 Feeds
  • guid, Permalink or not
  • Creating RSS 2.0 with PHP
  • Caching and saving



    Creating RSS 2.0 Feeds

    (Page 1 of 4 )

    In this conclusion to a three-part series on the RSS 2.0 specification, you'll learn how to create an RSS feed in several different ways. This article is excerpted from chapter four of the book Developing Feeds with RSS and Atom, written by Ben Hammersley (O'Reilly; ISBN: 0596008813). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Creating RSS 2.0 Feeds

    RSS 0.91 and 0.92 feeds are created in the same way; the additional elements found in 0.92 are well-handled by the existing RSS tools.

    Of course, you can always hand-code your RSS feed. Doing so certainly gets you on top of the standard, but it's neither convenient, quick, nor recommended.
    Ordinarily, feeds are created by a small program in one of the scripting languages: Perl, PHP, Python, etc. Many CMSs already create RSS feeds automatically, but you may want to create a feed in another context. Hey, you might even write your own CMS!

    There are various ways to create a feed, all of which are used in real life:

    XML transformation

    Running a transformation on an XML master document converts the relevant parts into RSS. This technique is used in Apache Axkit-based systems, for example.


    You can substitute values within a RSS feed template. This technique is used within most weblogging platforms, for example.

    An RSS-specific module or class within a scripting language

    This method is used within hundreds of little ad hoc scripts across the Net, for example.

    We'll look at all three methods, but let's start with the third, using an RSS-specific module. In this case, it's Perl's XML::RSS.

    Creating RSS with Perl Using XML::RSS

    The XML::RSS module is one of the key tools in the Perl RSS world. It is built on top of XML::Parser--the basis for many Perl XML modules--and is object-oriented. Actually, XML::RSS also supports the creation of the older versions of RSS, plus RSS 1.0, and it can parse existing feeds, but in this section we will deal only with its 2.0 creation capabilities.

    Incidentally, XML::RSS is an open source project. You can lend a hand, and grab the latest version, at http://sourceforge.net/projects/perl-rss.

    Examples 4-8 and 4-9 show a simple Perl script and the feed it creates.

    Example 4-8. A sample XML::RSS script


    use Warnings;
    use Strict;
    use XML::RSS;

    my $rss = new XML::RSS( version => '2.0' );

    title         => 'The Title of the Feed',
        link          => 'http://www.oreilly.com/example/',
        language      => 'en',
        description   => 'An example feed created by XML::RSS',
    lastBuildDate => 'Tue, 14 Sep 2004 14:30:58 GMT',
        docs          =>

    title         => 'Oreilly',
    url           => 'http://meerkat.oreillynet.com/icons/meerkat-powered.jpg',
    link          => 'http://www.oreilly.com/example/',
        width         => 88,
        height        => 31,
    description   => 'A nice logo for the feed'

        title         => "Search",
        description   => "Search the site",
        name          => "query",
        link          => http://www.oreilly.com/example/search.cgi

        title         => "Example Entry 1",
        link          => http://www.oreilly.com/example/entry1,
        description   => 'blah blah',

        title         => "Example Entry 2",
        link          => http://www.oreilly.com/example/entry2,
        description   => 'blah blah'

        title         => "Example Entry 3",
        link          => http://www.oreilly.com/example/entry3,
        description   => 'blah blah'

    print $rss->as_string;

    Example 4-9. The resultant RSS 2.0 feed

    <?xml version="1.0" encoding="UTF-8"?>

    <rss version="2.0" xmlns:blogChannel="http://backend.userland.com/ blogChannelModule">

    <title>The Title of the Feed</title> <link>http://www.oreilly.com/example/</link> <description>An example feed created by XML::RSS</description> <language>en</language>
    <lastBuildDate>Tue, 14 Sep 2004 14:30:58 GMT</lastBuildDate> <docs>http://blogs.law.harvard.edu/tech/rss</docs>

    <title>Oreilly</title> <url>http://meerkat.oreillynet.com/icons/meerkat-powered.jpg</url> <link>http://www.oreilly.com/example/</link> <width>88</width>
    <description>A nice logo for the feed</description>

    <title>Example Entry 1</title> <link>http://www.oreilly.com/example/entry1</link> <description>blah blah</description>

    <title>Example Entry 2</title> <link>http://www.oreilly.com/example/entry2</link> <description>blah blah</description>

    <title>Example Entry 3</title> <link>http://www.oreilly.com/example/entry3</link> <description>blah blah</description>

    <description>Search the site</description> <name>query</name> <link>http://www.oreilly.com/example/ search.cgi</link>

    After the required Perl module declaration, you create a new instance of XML::RSS, like so:

      my $rss = new XML::RSS (version => '2.0');

    The new method function returns a reference to the new XML::RSS object. The function can take three arguments, two of which are of interest here:

      new XML::RSS (version=>$version, encoding=>$encoding);

    The version attribute refers to the version of RSS you want to make (either '2.0' or '1.0', or, if you fancy being a bit retro, '0.91'), and the encoding attribute sets the encoding of the XML declaration. The default encoding, as with XML, is UTF-8.

    The rest of the script is quite self-explanatory. The methods channel, image, textinput, and add_item all add new elements and associated values to the feed you are creating, and the print $rss->as_string; prints out the result. You can also call the $rss->save method to save the created feed as a file.

    More XML Tutorials Articles
    More By O'Reilly Media

    blog comments powered by Disqus


    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - Getting to Know XML
    - A Friendly Approach to XML
    - Creating RSS 2.0 Feeds
    - Using Modules in Your RSS Feed
    - RSS 2.0
    - Querying XML: Use Cases
    - Joins and Query Use with XML
    - Solving Problems by Querying XML
    - Performing Set Operations When Querying XML
    - Querying XML
    - Handling Data for Ajax with JSON
    - Handling XML Data for Ajax
    - XML and JSON for Ajax

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap