Once i was looking for a nice RSS class/component for ASP which allows me to read all kind of feed formats programmatically. In addition to the correct parsing it should also be able to create own feeds for publishing purposes. Unfortunately I could not find any component which fits those requirements in ASP and therefore written my own.
Due to the different versions of RSS (0.91, 0.92, 1.0, 2.0, etc.) and formats like ATOM it was not really easy to parse them all within one component. I needed to find the least common denominator which consists of the basic elements like feed title, feed description, feed published date, the feeds language and it published items. The items on the other hand must hold at least title, description, category, author, link and published date. This metadata is available but its occurrence is a bit different in e.g. ATOM and RSS. The component I have written consists of two VBScript classes (RSS and RSSItem) which supports the following features (download and documentation is at the bottom):
- Reading all kind of RSS formats (RSS 0.91, 0.92, 0.94, 1.0, 2.0 and ATOM)
- Generating own feeds as RSS 2.0 (with UTF8)
- Generation can be done directly into a file
- XSLT can be applied to the feed
- Feeds can be cached for a given amount of time (all visitors share the same cache).
- Timeout for the request can be configured. (if server not responding, etc.)
- Supports the simple Dublin Core elements
- easy to use programmers interface
Before I keep going with the examples of how to use the class(es) here is a link to a demo I have quickly created on my ASP server. Play around and check if your feeds are being correctly recognized by the component.
RSS reader DEMO
Could the component parse your feed? If not please let me know so I can improve it. However, lets check how we use this tool. The following example creates an instance of the RSS class, requests the feed and loads all its information into the class’ properties. The title of the feed is written to the response:
- <!--#include file="RSS.asp"-->
- set r = new RSS
- r.url =
- r.load()
- if r.failed then
- response.write("Could not load the feed.")
- else
- response.write(r.title)
- end if
Interesting thing here is the failed property. Whenever a feed is requested it is necessary to check the failed property if the request has failed or not. If it failed it means that the feed could not be recognized as a valid (supported) feed, the server is not responding, the url does not exist, or the XML could not be parsed (and therefore is invalid). As we are requesting data from a kind of “untrusted” source it is a nice feature to display our user that the feed is currently not available. If the load was a success all properties are populated with the feeds data. Iterating through the items of the previous example looks the following:
- for each item in r.items.items
- response.write(item.title)
- response.write(month(item.publishedDate))
- next
Those lines iterate through the items (items is a dictionary which holds objects of type RSSItem). Nice feature here is that the date has already been converted into a VBScript date type and can be used within all date functions. In this example the month of the date is written out.
With this concept it is easily possible to request feeds and process them programmatically. E.g. storing them into the database, etc.
Using the cache
- <!--#include file="RSS.asp"-->
- <!--#include file="Cache.asp"-->
- set r = new RSS
- r.url = "https://www.webdevbros.net/feed/"
- r.setCache "h", 1
- r.load()
- if r.failed then
- response.write("Could not load the feed.")
- else
- response.write(r.description)
- end if
Caching can be turned on with the “setCache” method which requires the type of interval and the interval value (in the example 1 hour). This feed is stored now within the cache (see Caching class for ASP) on the server for 1 hour before it is requested again. All users share the same cache which means that only the first user will recognize the request and the others will already get it from the cache. The RSS class supports caching but it requires the Cache class for this feature. Just download it and include it like in the example.
Using XSLT for the feed
- <!--#include file="RSS.asp"-->
- set r = new RSS
- r.url = "https://www.webdevbros.net/feed/"
- r.draw("stylesheet.xsl")
- if r.failed then response.write("Could not load the feed.")
If you have your own XSL file for your feeds you can apply it with the draw() method. It grabs the feed, applies the given stylesheet and prints out the result to the response. As you can see there is again this check for the failed property. That is necessary because the same errors as with the load() method could happen. Additionally the transformations could have failed. It is also possible to use caching here, just as described before.
Generating an own feed (RSS 2.0)
- <!--#include file="RSS.asp"-->
- set r = new RSS
- r.title = "Freaky funky feed (FFF)"
- r.description = "another source for nonsense :)"
- r.publishedDate = now()
- r.language = "en"
- set it = new RSSItem
- it.title = "My first blog entry"
- it.description = "i am so freaky and can do <strong>html</strong>"
- it.publishedDate = dateAdd("d", -2, now())
- it.author = "f. freak"
- r.addItem(it)
- r.generate "RSS2.0", "/rss.xml"
This example generate a simple feed with one item. As you can see its necessary to use the RSSItem class. The generate() method generates a feed into a file called rss.xml. The method also returns the generated xmldom if you prefer to store the feed in a different way. E.g. you could output directly to the response with:
- r.generate("RSS2.0", empty).save(response)
Another example for the generation grabs an existing feed (in this case ATOM) and generates an RSS 2.0 version. Its somehow a tranformation which is therefore possible .. a nice side effect.
- <!--#include file="RSS.asp"-->
- set r = new RSS
- r.url = "https://www.webdevbros.net/feed/atom/"
- r.load()
- if not r.failed then r.generate "RSS2.0", "/rss.xml"
There is nothing to say more now Have fun with this component and feel free to use it. Just keep my credits … I am looking forward to any enhancements from you guys and to the feedback. I am not an expert on feeds and therefore you might have some suggestions, improvements, etc.
Greetings from vienna.
Download RSS 1.0
RSS 1.0 Documentation
DEMO
NOTE: RSS class is part of the ajaxed library now!
check ajaxed library for a newer version…
Hi Michal, thanks for this, im getting an error:
Microsoft VBScript runtime error ’800a01a8′
Object required: ‘lib’
/RSS.asp, line 163
Any ideas?!
Sam
hi sam, this class is extracted from my framework which also includes the possibility to throw custom errors. This error comes up because the class wants to call the throwError function which it cannot find. Just take a look into the line where the error is thrown to see the reason. In your case its “lib.throwError(“Title, Link and description are required.”)”. This means that if you want to generate a feed it is necessary to set the properties title, link and description of the feed.
Hi Michal,
I’ve tried making the url field into an array to make the tool aggregate rss feeds into one giant list. The issue I’m having is that the items collection seems to reset with each rss feed. Here are my modifications to the load function … do you know what else I would need to change to have this functionality work? - Thanks for anything.
if theCache is nothing then
for each address in url
loadXML(getXMLHTTPResponse(address))
next
else
for each address in url
if address “” then
cacheID = address & “|LOAD”
cachedItem = theCache.getItem(cacheID)
if cachedItem “” then
‘the cache for this method stores the data as xml and needs to be parsed again
loadXML(cachedItem)
else
‘load and save to cache
xmlResponse = getXMLHTTPResponse(address)
loadXML(xmlResponse)
theCache.store cacheID, xmlResponse
end if
end if
if failed then exit sub
select case getRSSType()
case “RSS1.0″
readRSS(“1.0″)
case “RSS2.0″
readRSS(“2.0″)
case “ATOM”
readATOM()
end select
next
end if
@john: why dont you just create an instance for each feed an merge the items together? for this you dont need to change any of the code…
Excellent work Michal. Now I just have to decide if I want to load the rss data into a db table for further “searching/filtering” or just slap the data into an array and do an old fashioned “do loop” for searching/filtering.
This is almost EXACTLY what I was looking for (I did NOT want to write it myself…gotta love GOOGLE, a lazy man’s paradise!)
Thanks again!
John, to make a giant list I’ll define an array and simply call a new rss for each source I want (looping thru each and loading in my array). From there I’ll call the array for the actual data.
as to set up to 5 number the items es. iMaxResults = 5 items
dany bay
Hy all,
To fix “lib.throwError” problem just add the following code in rss.asp:
public ErrMessage ”[string] Error Message
private Sub libthrowError(str)
ErrMessage = str
Response.Write ErrMessage
end sub
and then find & replace lib.throwError with libthrowError in the file…
Pingback: Web Dev Bros » Blog Archive » ajaxed 1.0 released
Hello Michel,
I’m using my own RSS class at the moment, but would like to have one that can handle more feed type, like your.
But when I tested one of my important feed-links in your demo page it failed.
I don’t know German but it looks like it’s having problems extracting the field.
Can you please have a look at this and email me back?
Here is the feed:
BRG Reine
I noticed that my xml tag was stripped in the last post…
I don’t know German but it looks like it’s having problems extracting the pubDate field.
reine i have to take a closer look into it .. but as i have recognized also IE does not recognized this feed. google reader does
i will look into it. thanks for your feedback
Michal, how is it going, have you found any solution to my problem?
Is this open source? In the code it says “see license.txt in the root” but there’s no such file in the .zip
yeah it totally is … you cannot find the license.txt because i have extracted the component from the ajaxed library .. just a small mistake. you might want to check the ajaxed library https://www.webdevbros.net/ajaxed/
Great work - but a small question back to the first comment:
i hav2 “set RSSFeed1 = new RSS” and “Set RSSFeed2 = new RSS”
any suggestion how i “merge” them into on and sort them by date?
Only solution in my mind: put every child of the xml in one array/new xml / Database and then sort the full array. But this will take a lot of processing time … ?
Urs i would suggest you make use of the sortedlist from .net inside ASP (check for details( and sort the items by date .. then you create another RSS feed. should work fine. some pseudocode
I haven tried the code so i am not sure if it works It could be that the sorted list cannot store ASP “objects”. in this case you would need to create a lookup dictionary….
Michal,
Great job on this work. Work smoothly on most RSS.
However, I tested on one RSS at your demo page , putting this:
The images aren’t showing up.
Do you think it will be easy to add base url in the next version of your RSS class?
@Danny: adding the base url should be not that big deal. However, I am really busy for the next time … If you have some time you could extend it and I would release it as a new version. how does that sound? greets
can you show the code for the demo using ajax and the rss reader? its hard to see what is passed back in the code with just the output many thanks - great code too
Michael -
I’m using your ASP RSS Reader and I’ve run into a little problem. When reading ATOM feeds there are multiple parameters for each item. I’m tring to get the link that is directed back to the item which has an attribute rel=’alternate’. Unfortunately the script pulls up the first LINK which is typically ‘rel=self’ which is a link back to the feed file. I’ve tried playing around with your script but can’t figure out a way to access this alternate link. Do you know if there is a way or do you have any other tools you can direct me to? Thank you very much for your help – Mike
Figured it out, for those that are interested here’s the solution:
RSS.asp in the readATOM section
replace this line:
.link = getAttribute(n, “link”, “href”)
with:
.link = getAttribute(n, “link[@rel='alternate']“, “href”)
Now when ATOM feeds are read, the returned link will be the link back to the URL of the item instead of the ATOM feed file.
Thank you for posting this. It has been very helpful.
I am having trouble with a feed that has an array of category fields for each -item.
I have tried adding an array definition to the dictionary, and that did not work. Also tried to enumerate the r.items an could not get that to work. I obviously don’t know how to reference the child array of item->category. Do you have a suggestion?
Lee, as far as I can see the class does not parse multiple categories when reading a feed. You would need to extend it.
Lee -
I don’t do anything with the category field so I haven’t worked on that problem. I was only able to figure out my last problem by scouring over code samples that I could dig up on the web. In my searching I don’t recall seeing anything that wold help you - sorry.
Found one that will not parse or load.
Brent
Hi Michal,
Great work on the RSS Feed! I am working with it and I had a quick question. How does one format the date attribute in the RSS feed? when I try to format it with a formatdatetime function it spits out an error.