Displaying an RSS feed using XMLDom and ASP
It happened again. I saw some really horrible examples today on how to show an RSS feed using XMLDom so I took another trip with the WayBackMachine to try to dig this article up. After figuring which id-number this post had on the old installation it wasn’t that hard.
Anyone remembered why I wrote this article the first time? Here is a quote from the original about why I wrote it.
Imagine a script that selects all nodes (XPath query was something like “//*”), not caring where in the document, then looping them thru looking at their names and guessing what they are and then adding them to four separate strings with some weird separator. After doing this, they split the strings into four arrays and started looping out the content. Oh please, it was horrible. No real sense of XML at all I’m afraid . I know there are better resources then that on the net, but I still felt I had to contribute with just one more.
Hopefully there are still people who need to learn this out there so I didn’t waste my time recovering it. Anyway, let’s get going
If you never heard of RSS earlier, you are either very ignorant to what happens on the web these days and/or you are stuck running Internet Explorer which lacks any built in support for it. It all comes down quite easy though. An RSS feed is an online news syndication in the XML format. The main idea is to have a standardized way to share news over the net. I for an instance offers my articles as an RSS feed so people easily will know when I’ve written something new. For those who want to dive deeper into RSS I would recommend “What Is RSS” over at XML.com.
I thought that it might also be a good idea to show a short RSS feed with only one news item in it so you know what source we are working with.
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My company RSS Feed</title>
<link>http://www.mysite.com/</link>
<description>My company extends products using XML</description>
<language>en-uk</language>
<pubdate>Fri, 16 Apr 2006 15:15:00 +0200</pubdate>
<item>
<title>New company started</title>
<link>http://www.mysite.com/news.asp?id=26</link>
<comments>http://www.mysite.com/comments.asp?id=26</comments>
<description>Here the description goes</description>
<pubdate>Fri, 16 Feb 2006 00:00:00 +0200</pubdate>
</rss>
This is a quite minimal feed document. It could have any number of “item” nodes aswell as it could have way more descriptive tags both for the channel and for the items. This is not how the feed for my site looks. I’m using a nice tool that generates everything for me and puts in 10 or 15 extra tags that are far from necessary. I feel that it would be quite an overkill to start out with that and I doubt that more then 5% of the RSS readers out there actually cares about all those tags anyway.
Anyway, lets get going. For those who have read my previous tutorials, there isn’t anything really new here, for those who haven’t, I would recommend reading XML, XMLDom and ASP Part 1 and XML, XMLDom and ASP Part 2.
dim xmlDom, nodeCol, oNode, oChildNode
set xmlDom = Server.CreateObject("MSXML2.Domdocument.4.0")
call xmlDom.setProperty("ServerHTTPRequest", true)
xmlDom.async = false
call xmlDom.load("http://www.beaverblogg.com/?feed=rss2")
if not xmlDom.documentElement is nothing then
set nodeCol = xmlDom.documentElement.selectNodes("channel/item")
for each oNode in nodeCol
Response.Write("<p>" & vbCrLf)
set oChildNode = oNode.selectSingleNode("title")
if not oChildNode is nothing then
Response.Write(" <h1>" & oChildNode.text & "</h1>" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("pubDate")
if not oChildNode is nothing then
Response.Write(" » " & oChildNode.text & "<br />" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("description")
if not oChildNode is nothing then
Response.Write(" " & oChildNode.text & "<br />" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("link")
if not oChildNode is nothing then
Response.Write(" <br />" & vbCrLf)
Response.Write(" <a href=""" & oChildNode.text & """ target=""_blank"">")
Response.Write(" Link to full article" & vbCrLf)
Response.Write(" </a>" & vbCrLf)
end if
Response.Write("</p>" & vbCrLf)
next
else
Response.Write("<p>" & vbCrLf)
Response.Write(" An error occured." & vbCrLf)
Response.Write("</p>" & vbCrLf)
end if
So lets start examining the code then.
dim xmlDom, nodeCol, oNode, oChildNode
set xmlDom = Server.CreateObject("MSXML2.Domdocument.4.0")
call xmlDom.setProperty("ServerHTTPRequest", true)
xmlDom.async = false
call xmlDom.load("http://www.beaverblogg.com/?feed=rss2")
This part is very almost a carbon copy of the first lines in my second article, I just added nodeCol, oNode, oChildNode and i to the variable declaration and set the async property to false. The async property tells if the dom should proceed in asynchronous or synchronous mode. Since we don’t want it to continue to execute without being done with the HTTP request, we set it to false (synchronous mode).
if not xmlDom.documentElement is nothing then
...
else
Response.Write("<p>" & vbCrLf)
Response.Write(" An error occured." & vbCrLf)
Response.Write("</p>" & vbCrLf)
end if
Next, we have a simple if statement to see if the document has a documentElement (root element, remember?). If not, we show a short error message.
set nodeCol = xmlDom.documentElement.selectNodes("channel/item")
for each oNode in nodeCol
...
next
If the script got this far, we probably got our RSS feed without any problems. Now we call the selectNodes function to select all item nodes from the feed and put the collection in nodeCol. Then, we do a simple for-each loop to go through all the nodes in the collection.
Response.Write("<p>" & vbCrLf)
set oChildNode = oNode.selectSingleNode("title")
if not oChildNode is nothing then
Response.Write(" <h1>" & oChildNode.text & "</h1>" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("pubDate")
if not oChildNode is nothing then
Response.Write(" » " & oChildNode.text & "<br />" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("description")
if not oChildNode is nothing then
Response.Write(" " & oChildNode.text & "<br />" & vbCrLf)
end if
set oChildNode = oNode.selectSingleNode("link")
if not oChildNode is nothing then
Response.Write(" <br />" & vbCrLf)
Response.Write(" <a href=""" & oChildNode.text & """ target=""_blank"">")
Response.Write(" Link to full article" & vbCrLf)
Response.Write(" </a>" & vbCrLf)
end if
Response.Write("</p>" & vbCrLf)
This might seem like a long chunk of code, but it’s quite repetive so I think you can handle it. First of all, we write some standard html markup. We encapsulate each item in a paragraph. Next, we try to select a childnode from the current node. Each item should have a title, a pubdate, a description and a link, but since people are lazy sometimes they leave one or several of them out so to make this as general as possible we make sure that the nodes exists to avoid a crash. Anyway, we try to selet any childnode named title and put it into the oChildNode variable. Then, if not oChildNode is nothing, we can ouput the text content of that node by using oChildNode.text. Again we are adding some markup to the code, this to make it easy to style it afterwards using CSS. We repeat the exact same thing for the other “supposed to be there” nodes and print them out accordingly. We even make a link out of the link node if it exists. And well, thats kinda it.
This post didn’t take long to write so I apologize if it contains any errors of some sort. If it do, then please tell me so I can correct them as fast as possible. It was quite fun though to join the stuff I’ve written about in the first to articles into this one and there will be more to come!