Displaying an RSS Feed using ASP
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
<>channel title
link to channel
description
en-us
item title
link url
unique id
item description
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", TruexmlDOM.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 - tags in the feed
Set itemList = xmlDOM.getElementsByTagName("item")
strHTML = strHTML & ""
'Iterate over each itemFor 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 & ""
strHTML = strHTML & "& Server.HTMLEncode(link) & "'>"
strHTML = strHTML & Server.HTMLEncode(title)
strHTML = strHTML & ""
strHTML = strHTML & "
"
strHTML = strHTML & description
strHTML = strHTML & "
"
strHTML = strHTML & ""Next
strHTML = strHTML & ""
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 - tags in the feed
Set itemList = xmlDOM.getElementsByTagName("item")
strHTML = strHTML & ""
'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 & ""
strHTML = strHTML & "& Server.HTMLEncode(link) & "'>"
strHTML = strHTML & Server.HTMLEncode(title)strHTML = strHTML & ""
strHTML = strHTML & "
"
strHTML = strHTML & description
strHTML = strHTML & "
"
strHTML = strHTML & ""
Next
strHTML = strHTML & ""
Set xmlDOM = Nothing
Set itemList = Nothing
Application.Lock
Application("rss-html") = strHTML
Application("rss-html-time") = Now()
Application.UnLockEnd If
Response.Write(Application("rss-html"))%></body>
</html>
No comments:
Post a Comment