mardi 3 mai 2011
RSS Feed: How to create custom feeds (part 2)
Do you like this story?
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>http://www.yourdomain.com</link>"
objtstream.writeline "<description>Your description</description>"
objtstream.writeline "<ttl>60</ttl>"
objtstream.writeline "<language>en-us</language>"
objtstream.writeline "<image><url>http://www.yourdomain.com/images/yourimage.jpg</url>"
objtstream.writeline "<title>Your title</title><link>http://www.yourdomain.com</link></image>"
objtstream.writeline "<copyright>Your copyright</copyright>"
vardata = return_RFC822_Date(Now(), "GMT")
objtstream.writeline "<lastBuildDate>"&vardata&"</lastBuildDate>"
objtstream.writeline "<pubDate>"&vardata&"</pubDate>"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>"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>"objtstream.writeline "<description>"&rs.fields.item("news_body").value&"</description>"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>"var_count = var_count + 1
rs.movenext()
wend objtstream.writeline "</channel>"
objtstream.writeline "</rss>"
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:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Feed Generator</title>
</head>
<body>
<div align="center">
  <p><strong>PLEASE WAIT</strong></p>
  <p>Processing Rss Feed creation</p>
</div>
</body>
</html>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!

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “RSS Feed: How to create custom feeds (part 2)”
Enregistrer un commentaire