Displaying an image from RSS Enclosure tag with ASP

MrBigJust
MrBigJust used Ask the Experts™
on
Hi People - I've been killing myself trying to solve this problem. The news feed that use on our website now gives images as part of it's feed. Only trouble is I don't know how to display the URL attribute in the <enclosure> tag.

Any help is appreciated.


<%
 Response.Expires = -1
 
 ' ##### URL to RSS Feed to display #########
 URLToRSS = "http://feeds.news.com.au/public/rss/2.0/news_the_nation_45.xml"
 
 ' ##### max number of displayed items #####
 MaxNumberOfItems = 1
 
 ' ##### Main template constants
 MainTemplateHeader = "<table>"
 MainTemplateFooter = "</table>"
 
 Keyword1 =  ""  ' Keyword1 =  "tech" - set non-empty keyword value to filter by this keyword
 Keyword2 = "" ' Keyword1 =  "win" - set non-empty keyword value to filter by this keyword
' #################################
 
 ' ##### Item template.
 ' ##### {LINK} will be replaced with item link
 ' ##### {TITLE} will be replaced with item title
 ' ##### {DESCRIPTION} will be replaced with item description
  '##### {ENCLOSURE_URL} will be replaced with Media URL
' ##### {ENCLOSURE_TYPE} will be replaced with Media title
' ##### {ENCLOSURE_SIZE} will be replaced with Media file size in KB
 ItemTemplate = "<tr><td><a href=" & """{LINK}""" & "target='_blank'><b><font size=3>{TITLE}</font></b></a><img src=" & "{ENCLOSURE_URL}" & "><BR>{DESCRIPTION}{ENCLOSURE_URL}</td></tr>"
 
 ' ##### Error message that will be displayed if not items etc
 ErrorMessage = "Error has occured while trying to process " &URLToRSS & "<BR>Please contact web-master"
 
 ' ================================================
 
 Set xmlHttp = Server.CreateObject("MSXML2.XMLHTTP.3.0")
 xmlHttp.Open "Get", URLToRSS, false
 xmlHttp.Send()
 RSSXML = xmlHttp.ResponseText
 
 Set xmlDOM = Server.CreateObject("MSXML2.DomDocument.3.0")
 xmlDOM.async = false
 xmlDOM.LoadXml(RSSXML)
 
 Set xmlHttp = Nothing ' clear HTTP object
 
 Set RSSItems = xmlDOM.getElementsByTagName("item") ' collect all "items" from downloaded RSS
 Set xmlDOM = Nothing ' clear XML
 
 RSSItemsCount = RSSItems.Length-1
 
 ' writing Header
 if RSSItemsCount > 0 then 
  Response.Write MainTemplateHeader
 End If
 
 j = -1
 
 For i = 0 To RSSItemsCount
 Set RSSItem = RSSItems.Item(i)
 
  for each child in RSSItem.childNodes
   Select case lcase(child.nodeName)
     case "title"
           RSStitle = child.text
     case "link"
           RSSlink = child.text
     case "description"
           RSSdescription = child.text
     case "enclosure"
           RSSenclosure =  child.text
	 case "enclosure_url"
           RSSenclosure_url =  child.text
	 case "enclosure_type"
           RSSenclosure_type =  at.text
   End Select
  next
 
' now check filter
 If (InStr(RSSTitle,Keyword1)>0) or (InStr(RSSTitle,Keyword2)>0) or (InStr(RSSDescription,Keyword1)>0) or (InStr(RSSDescription,Keyword2)>0) then
 
  j = J+1
 
  if J<MaxNumberOfItems then 
  ItemContent = Replace(ItemTemplate,"{LINK}",RSSlink)
  ItemContent = Replace(ItemContent,"{TITLE}",RSSTitle)
  ItemContent = Replace(ItemContent,"{ENCLOSURE}",RSSenclosure)  
   ItemContent = Replace(ItemContent,"{ENCLOSURE_URL}",RSSenclosure_url)   
  Response.Write Replace(ItemContent,"{DESCRIPTION}",RSSDescription)
 
  ItemContent = ""
  End if
End If 
 
 Next
 
 ' writing Footer
 if RSSItemsCount > 0 then 
  Response.Write MainTemplateFooter
 else 
  Response.Write ErrorMessage
 End If
 
' Response.End
%>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008

Commented:
...
     case "enclosure"
           RSSenclosure =  child.text
           RSSenclosureURL = child.getAttribute("url")
...

Author

Commented:
Hi Heilo,

unfortunately that doesn't seem to work
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is a copy of my RSS feed that includes an image for the channel, but not for the individual items.  It does not have an "enclosure" tag - it has an "image" tag with a URL.  The main difference seems to be that the enclosure tag will be turned into an array, whereas the image tag will be part of the object

Both Yours and Mine with image tag:
rss->channel->image->url

Yours with enclosure tag:
rss->channel->item->enclosure[type] =
rss->channel->item->enclosure[length] =
rss->channel->item->enclosure[url] =

I'm not ver good with VB, but maybe this will help you.  Best, ~Ray
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
<!-- -->
<!-- -->
<!--     IF YOU CAN SEE THIS, YOU NEED AN RSS READER!   -->
<!-- -->
<!-- -->
<channel>
  <title>National Trust Community Investment Corporation News</title>
  <link>http://www.NTCICFunds.com/</link>
  <description>NTCIC makes equity investments in real estate projects that qualify for historic tax credits.</description>
  <language>en-us</language>
  <pubDate>Tue, 26 Aug 2008 13:00:01 CDT</pubDate>
  
  <image>
    <url>http://NTCICFunds.com/images/ntcic_rss_logo.png</url>
    <title>NTCIC Web Site</title>
    <link>http://www.NTCICFunds.com/</link>
  </image>
  
  <item>
    <title>NTCIC News Updated on Tue, 26 Aug 2008 13:00:01 CDT!</title>
    <link>http://www.NTCICFunds.com/news/</link>
    <description>The RSS Feed from www.NTCICFunds.com has been updated.</description>
    <pubDate>Tue, 26 Aug 2008 13:00:01 CDT</pubDate>
  </item>
  
</channel>
</rss>

Open in new window

Expert of the Year 2008
Top Expert 2008
Commented:
I think this is what you want:
...
  for each child in RSSItem.childNodes
   Select case lcase(child.nodeName)
     case "title"
           RSStitle = child.text
     case "link"
           RSSlink = child.text
     case "description"
           RSSdescription = child.text
     case "enclosure"
           RSSenclosure =  child.text
	 'case "enclosure_url"
           RSSenclosure_url =  child.getAttribute("url")
	 case "enclosure_type"
           RSSenclosure_type =  at.text
   End Select
  next
...

Open in new window

Author

Commented:
Thanks Heilo!! Worked a dream!! You're a legend!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial