Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

RSS Feed - Need of the hour !

4.25/5 (13 votes)
7 May 2007CPOL6 min read 1   290  
This article is aimed to get familiar with RSS (Really Simple Syndication) Feed, its formation, practical usage and its progmatical implementation

Download understandingRSSFeed.zip - 0.9 KB

Introduction

If you have tired to see the same contents each day on your Webpage and it attracts no more to your friends or colleagues. You want to bring more traffic towards your website but you don't have time to change or update the contents by your self daily. Also, you are craving to share your own contents over the worldwide web and to let people know when your page contents are updated.

Then trust me, RSS (Really Simple Syndication) Feed is exactly going to do the same thing for you. Today, with the blessing of RSS, we can truly make our WebPages attractive by automatically fetching and updating various contents from different sources. Simultaneously we can publish various contents for the worldwide and all its going to cost just one time development effort.

Few usages of RSS:

1. To display latest news, headlines, events n happenings
2. To display daily Weather updates
3. To display Joke of the Day, Quote of the day
4. To be notified about your favorite Blogs (web logs) when updated
5. To display daily Horoscope

Where to Find RSS Feed:

It is not very difficult to search the RSS feed containing your favorite contents by using your favorite search engine such as using Google you can filter your search by adding "FileType:rss" with your actual search string and this will restrict your search to '.rss' files only.

Although this is not the Rule, but most of the Websites providing RSS Feed use the <place w:st="on">Orange button (such as Screenshot - rss_button.gif or Screenshot - xml_button.gif ) as a link to their RSS Feed.

Performance:

As RSS is nothing but just another XML file with some reserve tags so using RSS to populate your webpage takes not much time.

Validating RSS Feed:

RSS feed can be validated by writing your own utility or by checking the whole XML file elements manually. However there are tools available to validate whether the specified RSS Feed is correct or not. You can validate your Feed by one click from the sites below;

http://feedvalidator.org
http://validator.w3.org/feed

Cautions:

As RSS Feed is just another XML file so all the rules and regulations to make XML File are applicable to it such as it must be well formed and each opening tag must need to be closed.

When Not to use RSS Feed:

Avoid using RSS feed if required contents are updated infrequently. As it always read and parse XML file so if the changes are very occasional then using RSS may result just the wastage of server time and resources.

RSS Versioning:

Latest available version of RSS is 2.0 and it is backward compatible with its previous version i.e.; RSS 0.91. RSS 2.0 is much simpler and easier to use than its previous version.

Formation of RSS Feed:

RSS Feed is just like any other XML file except that it is created by following certain regulations such as each file must have <channel> element at the top most level. And each <channel> element must contain at least these three elements; <title>, <link>, <description>.

Also there are several optional elements that can come under the <channel> element; such as <image>, <pubDate>, <copyright>, <generator>, <category>, <cloud>, <webmaster>, <ttl>.

Element

Description

Required

<title>

Title of the channel such as the name of the webpage providing this RSS Feed

Yes

<link>

Hyperlink to the Webpage that is providing this channel

Yes

<description>

Brief details of the channel i.e.; the purpose of the webpage providing this channel

Yes

<image>

To display the image

No

<pubDate>

When this feed was published

No

<copyright>

To whom this RSS Feed copy righted to

No

<generator>

Tells that by which program/software the Feed has been generated.

No

<category>

Tells the category of the feed.

No

<cloud>

This notifies the registered processes when any update occurs in our feed.

No

<webmaster>

Webmaster email address can be defined here.

No

<ttl>

Stands for "Time to Live", defines the cached time of the feed.

No

<image> element also contains three required elements <url>,<title>,<link> while the other three elements <width>,<height>,<description> are optional.

Now where should we place our contents that are supposed to be changed frequently? For this we have another element named <item> under our <channel> element. Each <channel> usually contains one or more <item> elements. It can also be said that <item> element is the heart of a RSS Feed. An <item> contain three compulsory elements; <title>, <link>, <description>.

Element

Description

Required

<title>

Title to be displayed such as a news headline, today's joke title

Yes

<link>

Hyperlink to the page containing whole story

Yes

<description>

Brief details of the item.

Yes

<author>

Contains email address of the author of the item

No

<category>

This is used to organize the items by categories

No

<comments>

Contains link of the comments about particular item

No

<enclosure>

Used to include any media file with the item

No

<guid>

A unique identifier used to identify particular item

No

<pubDate>

Tells when the item was published

No

<source>

Contains the source of the item such as the item's information is coming from some where else

No

Publishing your RSS file:

Well to Syndicate the contents, we just need to have a valid RSS file and to create a link on our Webpage. Although, it is not a rule, but people use the orange buttons such as RSS Logo or XML Logo for this purpose. So either you can follow this de facto standard or you can link your RSS Feed with any of your favorite image or text such as below;

HTML
<a href="MyRSSFeed.rss"> <img src="images/myImage.gif" width="20" height="20"> </a>
<a type="application/rss+xml" href="MyFeed.rss"> My RSS Feed Link </a>   

Sample RSS Feed:

XML
<rss version="2.0">
   <channel>
      <title> Nawaz Ijaz's Blog </title>
      <link> http://blog.360.yahoo.com/blog-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1 </link>
      <description> 
       M a nightmare trying to live a dream! Unpredictable... Don't forget & forgive so  easily...!!
      </description>
      <language> en-us </language>
      <lastBuildDate> Fri, 09 Mar 2007 13:20:27 GMT </lastBuildDate>
    
      <item>
        <title> Bad patch continued for Tigerz... [Series Review: by Farhan Asad] </title>
        <link> http://blog.360.yahoo.com/blog-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1&p=9 </link>
        <description>
        The Epic continued as two arch rivals continued in a heated contest. Tigers came to
        the foreground with their oath of beating the Stallions mercilessly. But the Stallions
        went into battle with resounding confidence, substantiated with their continuous
        winning streak. <br /> <br />  The first match was a nail bitter. Stallions scored 75 runs
        in their quota of 8 overs, after struggling earlier on in the innings...
        </description>
        <pubDate>Tue, 06 Feb 2007 09:02:17 GMT</pubDate>
      </item>
    
      <item>
        <title> For All Kangro Lovers! </title>
        <link> http://blog.360.yahoo.com/blog-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1&p=8 </link>
        <description>
            Well friends if you like kangros then this is for you only :)<br/>
        </description>
        <pubDate> Thu, 01 Feb 2007 07:44:08 GMT </pubDate>
      </item>
     
      <item> 
        <title> Me @ MSN Spaces....!! </title>
        <link> http://blog.360.yahoo.com/blog-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1&p=1 </link>
        <description>
        Well... m already on MSN Spaces n feeling comfortable with dat... have aleady
        put lot ov stuff in ma MSN webpage... can u give me the reason to continue yahoo 360 simultanously??
        u can always meet me on my MSN Space... your feedback n comments are more than welcome... :)<br/>
        <br /><a href="nawaz.spaces.live.com">nawaz.spaces.live.com</a><br />
        </description>
        <pubDate>Fri, 17 Nov 2006 12:55:50 GMT</pubDate>
      </item>

   </channel> 
</rss>

Above piece of RSS Feed is taken from my personal Blog... you may check the RSS File in detail at the URL below;

<a href=""http://blog.360.yahoo.com/rss-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1"">"http://blog.360.yahoo.com/rss-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1"</a>

Parsing RSS Feed and fetching the required contents:

Now as we have seen that RSS Feed is just an XML file with well defined DTD so by making use of any parser in any programming language, we can extract the required information from this XML file.

Now, we are going to make a simple ASP 3.0 webpage where we will parse and extract the Title, Publication Date and the Link to read the details. Then we will display the extracted contents in two different ways.

Code Overview:

First, we will set the RSSFeed path as an input such as below;

VB.NET
const inputRSSFile ="http://blog.360.yahoo.com/rss-RFHidsM_erFb5Sks9KqfGlOOxPA-?cq=1" 

Then we will create a "serverxmlhttp" object to load the above RSSFeed.

VBScript
Set xmlhttp = Server.CreateObject("msxml2.serverxmlhttp") 

xmlhttp.Open "GET", inputRSSFile, false   
xmlhttp.Send  
set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument") 
objXMLDoc.async = false 
objXMLDoc.loadxml(xmlhttp.ResponseText) 


Now as the whole RSSFeed has been loaded into memory so we can extract our desired contents such as we are extracting the "Title, Publication Date and Link" over here;

VB.NET
for each ParentNode in objXMLDoc.documentElement.childNodes
    for each ChildN in ParentNode.ChildNodes

       if childN.nodeName= "item" then
          for each tmpNode in childN.ChildNodes

              if tmpNode.nodeName = "title" then
                  marqueeString= marqueeString & tmpNode.text & " "
                  response.write "<font size=""2"" face=""Verdana""><b>" & tmpNode.text & "</b></font><br>"
          end if

              if tmpNode.nodeName = "pubDate" then
                 marqueeString= marqueeString & "[" & tmpNode.text & "]" &"     "
              response.write "<font size=""1"" face=""Verdana"">" & tmpNode.text & "</font><br><br>"
              end if

              if tmpNode.nodeName = "link" then
                  marqueeString= marqueeString & "<a target=""_blank"" href=" & tmpNode.text & ">" & "[see details]" & "</a>" & " "
                 response.write "<font size=""1"" face=""Verdana"">" & "<a target=""_blank"" href=" & tmpNode.text & ">" & "[see details]" & "</a>" & "</font><br>"
          end if

          next
       end if
    next
next

The above piece of code is quite self explanatory. Simple Loops and If statements.. Do I need to explain it too? Okay, lets do this too :-) First, we find an "Item" tag and then we are looking for our desired "title, pubDate, link" tags here.
"response.write" directly displays the desired contents on out ASP webpage. While "marqueeString", keeps on concatenating the same contents. In the end, we are setting this "marqueestring" to dhtml marquee object such as below;

XML
<marquee scrolldelay="120" style="font-family: Verdana; font-size: 9pt">
    <%= marqueeString %>
</marquee>   

Example Output:

Below is the screenshots of our example's output. In "RSS Feed - Sample usage 01", we are displaying the contents inside <Div> tag and in "RSS Feed - Sample usage 02", a marquee (starting from right to left) has been used to display the same contents.

Screenshot - understandingRSSFeed.gif


Thats it!

Congratulation friends, we have learned what RSSFeed really is; and how we can make use of it... Cheers...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)