Creating a RSS feed in ASP



  • I recently suggested that it might be a nice idea to have a feed for the news on our corporate website. I pointed our webmaster to the RSS specification (which is a WTF
    in itself - why would anyone willingly choose that stupid RFC822 date format when 
    there's a easily parseable ISO format available?) and he got to work.
    This is what he came up with:

    createfeed.asp
    <html>
    <head></head>
    <% dim strXML
    strXML = ""<?xml version=""1.0"" encoding=""utf-8""?>"
    strXML = strXML & "<rss version=""2.0"">"
    strXML = strXML & "<channel>"
    strXML = strXML & "<title>News</title>"
    strXML = strXML & "<description>Our newsfeed</description>"

    dim RS
    RS = DBGetTable("select * from news")
    while not RS.eof
      strXML = strXML &"<item><title>" & RS("title") & "</title>"
      strXML = strXML & "<description>" & RS("text") & "</description>"
      strXML = strXML & "</item>"
      RS.moveNext
    wend
    RS.close
    strXML = strXML & "</channel></rss>"

    dim fs, file
    set fs = CreateObject("Scripting.FileSystemObject")
    set file = fs.CreateTextFile(Server.MapPath("rss.xml", true, false)
    file.WriteLine(strXML)
    file.Close
    %>

    <script>document.location.href = "rss.xml";</script>
    </html>

    (Typos and omissions mine, had to protect the guilty)

    For non-coders: He builds a xml document (via the tried-and-trusted string concatenation, no less), writes it to the disk, and then redirects the client to that xml file (via javascript of course; a server-side Response.Redirect() would have been to easy).

    The real WTF is that is actually worked.



  • Does it create the file like once a day, or every time the feed is requested.

    If its like once a day, i can see this as a way to cut down on the number of database hits.

     

    But, if its at every request. This should crash if 2 people try to access it at the same time, since only one person could write it at a time.

    Really i hate crap like this. A person i work with used to write like this, until i showed him how much easier it is just to write the file out directly to the user instead of redirecting it.  He would name the file "something" + randomnumbers. So there were normally over 1000 new files in dir it was saving too every day And mind you all of these 1000 files were exactly the same. 


     



  • Oh, at every request of course. The .asp file is linked as the news feed source.



  • I don't suppose it occurred to anyone to just do:

    Response.ContentType = "text/xml" 

    Response.Write strXML 

    Response.End 



  • @striker said:

    I pointed our webmaster to the RSS specification (which is a WTF
    in itself - why would anyone willingly choose that stupid RFC822 date format when
    there's a easily parseable ISO format available?)

    That was the worst thing you could find in the fetid pile of yak vomit that is labelled as the RSS "specification"?

    (It's not actually that bad - it's worse. But I don't know any adjectives that would truly do it justice. RSS needs to die, preferably in a great deal of pain)



  • @flinnb said:

    I don't suppose it occurred to anyone to just do:

    Response.ContentType = "text/xml" 

    Response.Write strXML 

    Response.End 

     

    would have been to easy AND efficient 



  • @striker said:

    (via javascript of course; a server-side Response.Redirect() would have been to easy).

    So, um, it recreates a feed when the .asp file is called... and produces a static .rss file. And then, asks the client to go to the .rss file. With JavaScript.

    Seriously the dumbest move [i]ever[/i]. This setup doesn't guarantee the generated static .rss file is fresh at all. If the client asks for it via the .asp, it has to support JavaScript - which most RSS readers won't actually support for security reasons.

    Yay.



  • @flinnb said:

    I don't suppose it occurred to anyone to just do:

    Response.ContentType = "text/xml" 

    Response.Write strXML 

    Response.End 



    Yeah, that's what I told him (after I had finished laughing). Actually, I told him to dump all that strXML stuff and just Response.Write() every line.



  • Sometimes there's actually a good reason to write stuff out to a file. Computing some pages may be computationally expensive, and if they're really popular (like, say, an RSS feed) then feeding people that file is cheaper than computing it on-the-fly every time.

    Of course, this code doesn't suffer from that sort of particular CPU-saving virtue.

    And, of course, there are often better caching solutions.



  • @striker said:

    Yeah, that's what I told him (after I had finished laughing). Actually, I told him to dump all that strXML stuff and just Response.Write() every line.

    Eh, I dunno. I'd rather write a subroutine (/function/method/whatever you want to call it) that generated the XML in a string, and Response.Write the results of that... in case one wanted to use the contents of the RSS feed elsewhere. Maybe that's not a problem here, but in general, code that prints/echos/writes every line instead of returning something useful is a PITA to re-use anywhere else, and I've spent more time than I care to converting line upon line of echo($foo) into string concatenation when updating code.



  • Agreed--when I first started doing development in PHP (yes it's an ugly, inconsistent Perl-like mess, but it's common and it works well) I used echo all the time.  It was until I started refactoring things into templates that I realized how bad echo was (editing every line of your source code when refactoring is a bad sign).  From then on, I would at the very least build an entire page before echoing it, if not return the string to a template.

    That said, a series of echoes would pale in comparison to the WTFs in this code.
     



  • @striker said:

    I recently suggested that it might be a nice idea to have a feed for the news on our corporate website. I pointed our webmaster to the RSS specification (which is a WTF
    in itself - why would anyone willingly choose that stupid RFC822 date format when
    there's a easily parseable ISO format available?)

    No to mention that there's actually half a dozen different versions of RSS, each one subtly incompatible with every other.

    Why didn't you point him to the ATOM spec? It's much, much saner.
    @flinnb said:

    I don't suppose it occurred to anyone to just do:

    Response.ContentType = "text/xml" 

    Response.Write strXML 

    Response.End 


    You're actually supposed to send RSS feeds with the "application/rss+xml MIME type.

    Except for MSIE who'll try to download the files, of course, but sane browsers such as Safari, Firefox (but not Camino) and Opera will suggest that you subscribe to the feed (and Firefox and Safari will even go as far as displaying the feed in a very nice way).


Log in to reply