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 is a very simple XML format. In the RSS feed, the <item> tag will repeat for news item. The following is a very simple example of an RSS feed. See the Resources below for complete RSS 2.0 specification.
<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 <item> tags. To start, all of the items are placed in a list by calling the getElementsByTagName() method. Once we have the list of items, we will iterate over the list and get the data for each item in the feed.
'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>
discuss this topic to forum
