RSS feeds from scratch

Posted by Matt Thommes on November 11, 2005 | Post type: Gain

RSS is a form of content syndication, excellent for tracking a large number of sites or for simple notifications.

With the surging popularity of "blogging" tools, such as WordPress and Movable Type, many users underestimate (and take for granted) the inclusion of RSS capabilities.

This article will show you how to create your own RSS feed, from scratch - to track any type of data or information.

There are many different types of syndication formats available, but this article will only focus on RSS 2.0.

A quick overview

RSS is simply an application of XML. Therefore, it's just an XML text file, with specific elements used. So, creating an RSS file is as simple as creating an HTML file (if you have experience with that).

Using your favorite text editor, create a file, and save it with an extension of .xml.

You'll want to put this file on your web server - somewhere intuitive, such as a directory named, "RSS", or "feeds," but of course, it will work anywhere you put it.

Once you've uploaded the file, you can start directing your visitors to that file - or, in the case of private feeds - you'll want to keep that information to yourself.

Step 1: Find the source

The first thing you need to do is figure out the source of your content. This could be anything from text you write out manually, or information coming from a database.

You'll need to continuously populate your RSS file with the latest information.

This can be cumbersome at best - so it's helpful if your information is coming from a database, so the RSS file is updated automatically.

Step 2: Create the file

The actual structure of an RSS file is rather simple. Different syndication formats (0.91, 0.92, 1.0, 2.0, Atom, etc.) call for different elements - but sticking with the RSS 2.0 specification things should be a breeze.

In your text document, start out with the XML declaration.

This line is very important. Make sure it's at the very top of the document, with no blank lines before it - and make sure it's flush up against the left side of the page (no spaces before it):

A screen shot of a text editor

Next, we declare our application of RSS:

<rss version="2.0">

Inside the <rss> element are all the elements that help describe our content. By using intuitive elements, we are describing our content - so that it becomes much easier to understand (to the naked eye).

Here's what we have, so far:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">


</rss>

Next comes the <channel> element:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">

<channel>

</channel>

</rss>

The <channel> element contains all of the information associated with our feed.

We start off by providing information about our feed, using self-descriptive tags:

<title> A short, descriptive title for this feed </title>
<link> http://www.mysite.com/ </link>
<description> An interesting tag-line or description of this feed </description>
<language> en-gb </language>
<copyright> Copyright 2005, My Company, Inc. </copyright>

<webMaster> myemail@mysite.com </webMaster>

<image>
    <title> A brief title for your image </title>
    <url> http://mysite.com/images/rss.jpg </url>
    <link> http://www.mysite.com/ </link>
    <width> 88 </width>
    <height> 31 </height>
    <description> A brief description of your image </description>
</image>

You can change all the bold information between the tags, to reflect your own data.

A few things to note about the tags: <link> is a URL to the HTML web site, which corresponds with the feed.

<image> can contain either JPG, GIF, or PNG image file. This image is "affiliated" with your feed - much like a logo, or emblem. The <width> defaults to 88 pixels, but can be a maximum of 144 pixels. The <height> defaults to 31 pixels, but can be a maximum of 400 pixels.

Next, each "item" should be listed in chronological order - from most recent to least recent - with the most recent on top, and the least recent on the bottom.

An "item" is a piece of information that you wish to syndicate. This could be anything from web site news updates, to your own personal checking account information.

Using a "blog," as an example - each blog post would be a new item.

Each item is wrapped in an <item> tag. Everything inside the <item> tag represents information associated with that particular item. You can have as many <item> tags as necessary, to reflect the most recent information.

<item>
    <title> Distinct title for this item </title>

    <link> http://www.mysite.com/item-permalink/ </link>
    <pubDate> Thu, 27 Oct 2005 13:55:46 CST </pubDate>
    <category> Category of this item </category>
    <description>
        <![CDATA[
    
        <p>Actual output goes here.</p>
    
        ]]>
    </description>
</item>

As you can see, each <item> has specific tags associated with it, just like the main feed does. Again, the bold text you can change, to match your own data.

A few things to note about the tags: <link> is a permanent link to the actual item's source at the HTML web site. <pubDate> is the date and time this "item" was posted, and must be in the format you see above (exactly as you see above). <description> contains the actual HTML (or other) output. In order to ensure that the .xml file renders properly, you must not forget the <![CDATA[ ]]> tags.

Again, you can include as many <item> elements as you need. Just stack them on top of each other:

<item>

    ...

</item>

<item>

    ...

</item>

<item>

    ...

</item>

Here is the full .xml file, with two <item> elements, as an example:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">

    <channel>
    
        <title> A short, descriptive title for this feed </title>
        <link> http://www.mysite.com/ </link>
        <description> An interesting tag-line or description of this feed </description>
        <language> en-gb </language>
        <copyright> Copyright 2005, My Company, Inc. </copyright>
        
        <webMaster> myemail@mysite.com </webMaster>
        
        <image>
            <title> A brief title for your image </title>
            <url> http://mysite.com/images/rss.jpg </url>
            <link> http://www.mysite.com/ </link>
            <width> 88 </width>
            <height> 31 </height>
            <description> A brief description of your image </description>
        </image>
        
        <item>
            <title> Distinct title for this item </title>
        
            <link> http://www.mysite.com/item-permalink/ </link>
            <pubDate> Thu, 27 Oct 2005 13:55:46 CST </pubDate>
            <category> Category of this item </category>
            <description>
                <![CDATA[
            
                <p>Actual output goes here.</p>
            
                ]]>
            </description>
        </item>

        <item>
            <title> Distinct title for this item </title>
        
            <link> http://www.mysite.com/item-permalink/ </link>
            <pubDate> Thu, 27 Oct 2005 13:55:46 CST </pubDate>
            <category> Category of this item </category>
            <description>
                <![CDATA[
            
                <p>Actual output goes here.</p>
            
                ]]>
            </description>
        </item>
    
    </channel>

</rss>

Step 3: Subscribe to your feed!

Once you have completed editing your .xml file, you can upload it to your web server space, as mentioned in the overview.

Then, point your web browser to that file. This should be something like:

http://www.mysite.com/feed/myfeed.xml

Once there, copy and paste that URL, and plug it into your RSS reader, and start enjoying the convenience of RSS notifications!

Further reading

For further reading, check out the official RSS 2.0 Specification.

About the author(s)

Matt Thommes is an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Comments

Note: Comments may be viewed by authors, but if you have a more specific question you'd like to ask them, please email matt.thommes@paininthetech.com.

# KevinB at 9/3/2006 12:35 pm cst
This tutorial was a great help to me--Thanks!!

Quick Link to this comment: http://TTIP.me/c4091

# L.J. Wheeler at 9/30/2006 5:54 pm cst
I am very interested in putting together a custom RSS feed on one of my web sites and this article appears to be of value for accomplishing such a goal. However, when trying to view "Step 2", I keep encountering the following error message instead of the actual "Step 2" information: Parse error: syntax error, unexpected TSTRING in /home/paininth/publichtml/includes/common.inc(1175) : eval()’d code on line 9 Any assistance in obtaining the complete documentation pertaining to this subject would be greatly appreciated. Many regards, LJWheeler

Quick Link to this comment: http://TTIP.me/c4138

# Matt Thommes at 10/1/2006 5:58 am cst
Hi L.J. Sorry about that error message. It should be working fine now. It was a code sample that was causing the error. If you have any other questions, please let us know. And thanks for reading.

Quick Link to this comment: http://TTIP.me/c4140