Search code examples
xmlasp-classic

How to parse a reddit rss feed in classic asp?


I was hoping this question has been answered over the years, but it has not. I am trying to parse Reddit RSS feeds using classic ASP but I cannot parse through the thread. I can get it to load, but parsing it is impossible for me so, far.

I am trying to parse any rss feed, such as https://www.reddit.com/r/worldnews.rss

I'm using the following script:

rss_url ="https://www.reddit.com/r/worldnews.rss"
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
    Err.Clear ' shouldn't be needed; can't hurt
    'ON ERROR RESUME NEXT
xml.open "GET", rss_url, False
xml.send
    'ON ERROR GOTO 0
    If Err.Number <> 0 Then
        Response.Write "NO feed from ..."
    Else
 ResponseXML = xml.responseText
 'response.write "<hr>"&ResponseXML&"<hr>"
Set doc = Server.CreateObject("Microsoft.DOMDocument")
doc.loadXML( xml.ResponseXML.xml )

Set items = doc.getElementsByTagName("entry")

For inum = 0 To items.length-1 
    Set curitem = items.entry(inum)
    title = Replace( curitem.SelectSingleNode("title").text, "'", "''" )
    content = Replace( curitem.SelectSingleNode("content").text, "'", "''" )
    Set linkNode = curitem.SelectSingleNode("link") 
    If linkNode Is Nothing Then 
        link = "**NONE**" ' if no description given, supply this 
    Else 
        link = Replace( linkNode.text, "'", "''" ) 
    End If 
    'link =  Replace( curitem.SelectSingleNode("link").text, "'", "''" ) 
    Set descNode = curitem.SelectSingleNode("description") 
    If descNode Is Nothing Then 
        description = "**NONE**" ' if no description given, supply this 
    Else 
        description = Replace( descNode.text, "'", "''" ) 
    End If
         response.write "<strong>"& title & "</strong><br>"& description &"<br>"& content &"<br>"& link &"<hr><br><br>"
Next

The feed is loaded but I don't think it's loading into the dom. I'm not sure what is wrong.


Solution

  • rss_url ="https://www.reddit.com/r/worldnews.rss"
    Set xml = Server.CreateObject("Microsoft.XMLHTTP")
        Err.Clear ' shouldn't be needed; can't hurt
        'ON ERROR RESUME NEXT
        xml.open "GET", rss_url, False
        xml.send
        'ON ERROR GOTO 0
        If Err.Number <> 0 Then
            Response.Write "NO feed from ..."
        Else
     ResponseXML = xml.responseText
     'response.write "<hr>"&ResponseXML&"<hr>"
     'response.end
    
    Set doc = CreateObject("MSXML2.DOMDocument")
    doc.loadXML( ResponseXML )
    
    Set items = doc.getElementsByTagName("entry")
    
        For inum = 0 To items.length-1 
            Set curitem = items(inum)
            title = Replace( curitem.SelectSingleNode("title").text, "'", "''" )
            content = Replace( curitem.SelectSingleNode("content").text, "'", "''" )
            Set linkNode = curitem.SelectSingleNode("link") 
            If linkNode Is Nothing Then 
                link = "**NONE**" ' if no description given, supply this 
            Else 
                link = Replace( linkNode.text, "'", "''" ) 
            End If 
            'link =  Replace( curitem.SelectSingleNode("link").text, "'", "''" ) 
            Set descNode = curitem.SelectSingleNode("description") 
            If descNode Is Nothing Then 
                description = "**NONE**" ' if no description given, supply this 
            Else 
                description = Replace( descNode.text, "'", "''" ) 
            End If
                 response.write "<strong>"& title & "</strong><br>"& description &"<br>"& content &"<br>"& link &"<hr><br><br>"
        Next
    end if