ASP: Easily Parse and Consume RSS in Classic ASP

This script should work right out of the box..

<%
Call getNews(10)

Sub getNEWS(howManyResults)
myRSSfile = "http://rss.news.yahoo.com/rss/tech"

Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.4.0")
xmlHttp.Open "Get", myRSSfile, false
xmlHttp.Send()
myXML = xmlHttp.ResponseText

Set xmlResponse = Server.CreateObject("MSXML2.DomDocument.4.0")
xmlResponse.async = false
xmlResponse.LoadXml(myXML)
Set xmlHttp = Nothing

Set objLst = xmlResponse.getElementsByTagName("item")
Set xmlResponse = Nothing

intNoOfHeadlines = objLst.length -1

For i = 0 To (intNoOfHeadlines)
Set     objHdl = objLst.item(i)

for each child in objHdl.childNodes
Select case lcase(child.nodeName)
    case "title"
          title = child.text
    case "link"
          link = child.text
    case "description"
          description = child.text
     'You can also use the following: author,category,comments,enclosure,guid,pubDate,source
End Select
next

   kk = kk+1
     if kk < howManyresults+1 then
    Response.Write "<br /><a href=""" & link & """>" & title & "</a> <br /> " & description

    end if

Next
End Sub
%>

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in IIS
8 comments on “ASP: Easily Parse and Consume RSS in Classic ASP
  1. Rolf says:

    Nice little example of XML parsing…couple of things I noticed;

    myXML = xmlHttp.ResponseText

    xmlResponse.LoadXml(myXML)

    by loading the responseText into a variable you immediately convert the encoding into UTF-8..fine for most US feeds but anything with a UK pound sign (£) in it wil fail even though its a valid character…many of these feeds are give an ISO-8859-1 charset encoding.

    Can do;
    xmlResponse.Load xmlHttp.responseXML

    in one step instead…however this means that you will not be able to do any processing on the XML if it is formed badly..such as the notoriously terrible feeds from TheRegister which are full of unescaped &#38;chars and html tags etc.

    Thanks
    Rolf

  2. Super, but it would be nice to see some XSLT transformations, purely from a style perspective. Other than that, good stuff.

  3. sleepytom says:

    When I try to run this code I get the following error:

    error ‘80004005’

    Unspecified error

    Any ideas on how I could correct this?

    Thanks

  4. Maciej says:

    Nice piece of code. Thanks curly!

  5. C.J. Land says:

    Great code. Another comment is that if you are on IIS 6, you may have to change the references to version 4 to version 6 to get it to work.

    Set xmlHttp = Server.CreateObject(“Msxml2.ServerXMLHTTP.6.0”)
    xmlHttp.Open “Get”, myRSSfile, false
    xmlHttp.Send()
    myXML = xmlHttp.ResponseText

    Set xmlResponse = Server.CreateObject(“MSXML2.DomDocument.6.0”)
    xmlResponse.async = false
    xmlResponse.LoadXml(myXML)
    Set xmlHttp = Nothing

  6. sandy says:

    not working. hangs up browser.

  7. Now how about showing the exact OPPOSITE of this, and which is much more useful, and that is…an example in classic ASP that shows how to actually ADD a blog entry (as an item) or EDIT an <item> in the XML feed file. This would be extremely useful for folks that wrote their own blog pages in classic ASP with a SQL DB back end.

  8. taylor says:

    thanks! I have added some code to parse the image associated with the content as well.

    in the case statement add
    case “content:encoded”
    imageUrl = parseImageURL(child.text)

    and add it to the Response.Write call the same way
    “..>”

    and finally the definition of the function
    Function parseImageURL(text)
    Set regEx = New RegExp
    regEx.Pattern = “]+)[“”‘]?.*?>”
    regEx.Global = True

    On Error Resume Next
    parseImageURL = regEx.Execute(text)(0).Submatches(0)
    On Error Goto 0
    End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

*