Wednesday, 4 May 2011

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 "<?xml version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"UTF-8"&chr(34)&"?>"
objtstream.writeline "<rss version="&chr(34)&"2.0"&chr(34)&">"
objtstream.writeline "<channel>"
objtstream.writeline "<title>Your title</title>"
objtstream.writeline "<link></link>"
objtstream.writeline "<description>Your description</description>"
objtstream.writeline "<ttl>60</ttl>"
objtstream.writeline "<language>en-us</language>"
objtstream.writeline "<image><url></url>"
objtstream.writeline "<title>Your title</title><link></link></image>"
objtstream.writeline "<copyright>Your copyright</copyright>"
vardata = return_RFC822_Date(Now(), "GMT")
objtstream.writeline "<lastBuildDate>"&vardata&"</lastBuildDate>"
objtstream.writeline "<pubDate>"&vardata&"</pubDate>"
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 "<item>"
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 "<title>"&rs.fields.item("news_title").value&"</title>"
objtstream.writeline "<link>"&rs.fields.item("news_link").value&"</link>"
objtstream.writeline "<guid>"&rs.fields.item("news_link").value&"</guid>"
We inserted the title and the link. <guid> 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 "<description>"&rs.fields.item("news_body").value&"</description>"
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 "<pubDate>"&vardatain&"</pubDate>"
objtstream.writeline "</item>"
We then close the repeat region, after adding 1 to var_count:
var_count = var_count + 1
Finally we close the channel, the rss and the objectstream:
objtstream.writeline "</channel>"
objtstream.writeline "</rss>"

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:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Feed Generator</title>

<div align="center">
  <p><strong>PLEASE WAIT</strong></p>
  <p>Processing Rss Feed creation</p>
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!

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.