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.
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.
So lets start examining the code then.
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).
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.
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.
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!
Great article
How does this work with xml namespaces? I’m trying to get the “dc:creator” field out of a wordpress rss2 script but it wont accept the namespace..
I was wondering if you might have a snippet of code to pull a random node from the XML output? I have been searching for code on how to do that with ASP, but so far I havent found anything.
Can someone explain how I can remove items from the XMLDom?
When I get all the items with xmlDOM.getElementsByTagName(”item”), I want to delete all double authors, because I need to create a list of unique authors.
I’ve been trying and trying, but can’t find the solution,.. it sounds like it must be real simple.
I tried this: http://www.devguru.com/Technologies/xmldom/QuickRef/node_removeChild.html
and different combinations,.. it ends up in errors like this Object doesn’t support this property or method: ‘removeChild’
(I use MSXML2.Domdocument.3.0)
Someone can point me in the right direction?
I want a list of unique items. The rss feed I’m importing uses title for authors.
Hello Karl
How would I use this feed with Flash MX 2004. That is, how would I export it so that I could view it in a Flash movie?
Thanks
Steve