RSS Feed: How to create custom feeds (part 2)

In the previous part we have introduced the basics for the creation of custom RSS feeds. We ended the article creating the xml object and we were ready to write down the feed itself. Please, before going on here, see the first article if you haven't, otherwise you won't understand the following explanations.

The  Feed
First of all, we start creating the head of the feed. Just for explanatory purpose, let me say that a feed needs some basic elements:
1) a version;
2) a title;
3) a ttl (time to live);
4) a language;
5) if you like, a copyright statement;
6) a publishing date.

Let's create the first part of the feed with those information, which are someway general and not the news items themselves.
After the creation of the filesystemobject, add the following code:
objtstream.writeline ""
objtstream.writeline ""
objtstream.writeline ""
objtstream.writeline "<i>Your title</i>"
objtstream.writeline "http://www.yourdomain.com"
objtstream.writeline "Your description"
objtstream.writeline "60"
objtstream.writeline "en-us"
objtstream.writeline "http://www.yourdomain.com/images/yourimage.jpg"
objtstream.writeline "<i>Your title</i>http://www.yourdomain.com"
objtstream.writeline "Your copyright"
vardata = return_RFC822_Date(Now(), "GMT")
objtstream.writeline ""&vardata&""
objtstream.writeline ""&vardata&""
All the parts in italics should be customized by you. Please change them according to your needs and information. Remember that the above snippet should be pasted just after the code provided in the previous article.
Let's see what we are doing here.
The first 2 lines are actually stating the feed version. After that we open the channel tag and we put some general information about the feed itself:
1) the title, a general link to your web page and a description of the feed;
2) the ttl (time to live). It specifies the number of minutes the feed can stay cached before refreshing it from the source. We set it to 60;
3) the language is optional. Here you can find the list of rss language codes;
4) an image, another title, link and a copyright - those are completely optional;
5) a last build date and a publication date. In our example, those two dates are the same and they are converted using the RFC822 format date function explained in the previous part of this article. The actual date used is obtained with the asp function Now().

The news items
After creating the heading of our feed we start inserting the news items.
We create a repeat region and set the number of items to be inserted.
while (var_count < 15) and (not rs.eof)

objtstream.writeline ""
The var_count (which we set to 0 at the beginning) will count the items to be inserted into the feed. In the example is set to 15. That means that no more than 15 items will be inserted. Remember that, when you get your data from the database: in fact you should order or filter the recordset according to which items you want to be inserted into the feed (for example order them by date).
Now let's insert the news items.
objtstream.writeline ""&rs.fields.item("news_title").value&""
objtstream.writeline ""&rs.fields.item("news_link").value&""
objtstream.writeline ""&rs.fields.item("news_link").value&""
We inserted the title and the link. is optional and it defines a unique identifier for the item. In the example we use the link again but you could use the unique identifier of your table.
objtstream.writeline ""&rs.fields.item("news_body").value&""
With the above line, we set the actual news (the text). Remember that you can style the news text according to your needs adding the appropriate html tags.
We then add the news publication date and close the item tag:
vardatain = return_RFC822_Date(rs.fields.item("news_date").value, "GMT")
objtstream.writeline ""&vardatain&""
objtstream.writeline ""
We then close the repeat region, after adding 1 to var_count:
var_count = var_count + 1
rs.movenext()
wend
Finally we close the channel, the rss and the objectstream:
objtstream.writeline ""
objtstream.writeline ""

objtstream.close
Set objtstream = nothing
Set objfso = nothing
%>

The HTML part
The main part is all above. You can then add a little html section to your generate.asp page to make it more  user friendly. Add the following snippet just after the rest of the code:



Feed Generator




 

PLEASE WAIT


 

Processing Rss Feed creation




After that, remember to close your recordset connection.

When you will run the page, the xml file (news.xml) will be created into the rss folder. Open it and check if everything's ok. Now you have a news RSS feed!

That's all. Hope you find this two parts article useful.
 Please share your thoughts in the section below!