This tutorial will walk you through adding dynamic content from an RSS 2.0 data feed. RSS is a XML format for syndicating news content, web site updates, and blogs. Learn how to add this content to your web site with ASP.
RSS 2.0 Format
The RSS 2.0 specification is a very simple XML format. In the RSS feed, the
<rss version="2.0">
<channel>
<title>channel title</title>
<link>link to channel</link>
<description>description</description>
<language>en-us</language>
<item>
<title>item title</title>
<link>link url</link>
<guid>unique id</guid>
<description>item description</description>
</item>
</channel>
</rss>
Reading the RSS Feed
To read the RSS feed, the MSXML2.DOMDocument object is used to read and parse the XML. This object allows for the XML DOM (Document Object Model) to be easily accessed.
The following example read an RSS feed into the XML DOM object:
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("http://www.codebeach.com/rss-it-jobs.asp")
Parse the news items
The information in the RSS feed that we are interested in is within the
'Get all of the <item> tags in the feed
Set itemList = xmlDOM.getElementsByTagName("item")
strHTML = strHTML & "<ul>"
'Iterate over each item
For Each item In itemList
'Parse the item children
For each child in item.childNodes
Select case lcase(child.nodeName)
case "title"
title = child.text
case "link"
link = child.text
case "description"
description = child.text
End Select
Next
'Build the HTML for each bullet item
strHTML = strHTML & "<li>"
strHTML = strHTML & "<a href='" & Server.HTMLEncode(link) & "'>"
strHTML = strHTML & Server.HTMLEncode(title)
strHTML = strHTML & "</a>"
strHTML = strHTML & "<br>"
strHTML = strHTML & description
strHTML = strHTML & "<br> "
strHTML = strHTML & "</li>"
Next
strHTML = strHTML & "</ul>"
Set xmlDOM = Nothing
Set itemList = Nothing
Response.Write(strHTML)
Final Version
Below is the complete version of the example to read the RSS 2.0 feed. The final version will also cache the HTML content built on the RSS feed. This is so it won't get the RSS feed everytime the web page is loaded. By caching the resulting HTML, it will improve the page performance and it won't overload the server that you are requesting the feed from. To view more information on caching, you may want to read the tutorial Caching Data in ASP.
<%@ Language="VBScript" %>
<html>
<head>
<title>RSS Reader</title>
</head>
<body>
<%
If DateDiff("h", Application("rss-html-time"), Now()) >= 2 then
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("http://www.codebeach.com/rss-it-jobs.asp")
'Get all of the <item> tags in the feed
Set itemList = xmlDOM.getElementsByTagName("item")
strHTML = strHTML & "<ul>"
'Iterate over each item
For Each item In itemList
'Parse the item children
For each child in item.childNodes
Select case lcase(child.nodeName)
case "title"
title = child.text
case "link"
link = child.text
case "description"
description = child.text
End Select
Next
'Build the HTML for each bullet item
strHTML = strHTML & "<li>"
strHTML = strHTML & "<a href='" & Server.HTMLEncode(link) & "'>"
strHTML = strHTML & Server.HTMLEncode(title)strHTML = strHTML & "</a>"
strHTML = strHTML & "<br>"
strHTML = strHTML & description
strHTML = strHTML & "<br> "
strHTML = strHTML & "</li>"
Next
strHTML = strHTML & "</ul>"
Set xmlDOM = Nothing
Set itemList = Nothing
Application.Lock
Application("rss-html") = strHTML
Application("rss-html-time") = Now()
Application.UnLock
End If
Response.Write(Application("rss-html"))
%>
</body>
</html>
7 comments:
Thanks this worked great.
Thanks - searched ages for something simple and repeatable like this....
Thanks! Worked nice! I really appreciate the cut'n paste scripts on the web!!! But, since I don't speak ASP very well I need help to make this display only a certain number of feeds. And while you're at it, why not throw in a limit on the feeds length. ;) Thanks again!
Regards!
/Richard
This is just great and works fine. I am a beginner and would like some help. If the feed has too many items, how do i display, say, 10 items per page with page scrolling for the rest of the pages?
Thank you.
thanks, its very helpfull, can you guide my how to pick channel details?
Thanks for this great article
Post a Comment