Limiting Output of RSS Feed

Ask a Question related to Coldfusion - Getting Started, Design and Development.

  1. #1

    Default Limiting Output of RSS Feed

    I am pulling an RSS feed into an intranet and have not figured out how to
    restrict the output to the 10 most recent news items. I know I can do a TOP 10
    or something similar from my own database, but how do I do this with an RSS
    feed? Here is the code:

    <cfhttp url="http://www.nih.gov/news/feed.xml"/> <cfset rss =
    XMLParse(cfhttp.filecontent)>
    <!--- get an array of items --->
    <cfset items = XMLSearch(rss, "/rss/channel/item")>
    <cfset channel = XMLSearch(rss, "/rss/channel")>
    <cfset rssItems = QueryNew("title,description,link")>
    <!--- loop through the array of items --->
    <cfloop from="1" to="#ArrayLen(items)#" index="i">
    <cfset row = QueryAddRow(rssItems)>
    <cfset title = XMLSearch(rss, "/rss/channel/item[#i#]/title")>
    <cfif ArrayLen(title)> <cfset title = title[1].xmlText>
    <cfelse>
    <cfset title="">
    </cfif>
    <cfset description = XMLSearch(items, "/rss/channel/item[#i#]/description")>

    <cfif ArrayLen(description)>
    <cfset description = description[1].xmlText>
    <cfelse>
    <cfset description="">
    </cfif>
    <cfset link = XMLSearch(items, "/rss/channel/item[#i#]/link")>
    <cfif ArrayLen(link)>
    <cfset link = link[1].xmlText>
    <cfelse>
    <cfset link="">
    </cfif>
    <!--- add to query --->
    <cfset QuerySetCell(rssItems, "title", title, row)>
    <cfset QuerySetCell(rssItems, "description", description, row)>
    <cfset QuerySetCell(rssItems, "link", link, row)>
    </cfloop>
    <cfset chtitle = XMLSearch(rss, "/rss/channel/title")>
    <cfif ArrayLen(chtitle)>
    <cfset ctitle = chtitle[1].xmlText>
    <cfelse>
    <cfset ctitle="">
    </cfif>
    <cfset chdate = XMLSearch(rss, "/rss/channel/pubDate")>
    <cfif ArrayLen(chdate)>
    <cfset date = chdate[1].xmlText>
    <cfelse>
    <cfset date="">
    </cfif>
    <cfset img = XMLSearch(rss, "/rss/channel/image/url")>
    <cfif ArrayLen(img)>
    <cfset imgurl = img[1].xmlText>
    <cfelse>
    <cfset imgurl="">
    </cfif>
    <cfoutput> <table cellpadding="5" cellspacing="1">
    <tr><td><strong> #ctitle# | #date# </strong></td></tr>
    </cfoutput>
    <cfoutput query="rssItems">
    <tr><td><b><a href="#rssItems.link#">#rssItems.title#</a></b> -
    #rssItems.description#</td></tr>
    </cfoutput>
    </table>

    HMOKeefe Guest

  2. Similar Questions and Discussions

    1. Limiting available CSS styles
      We're testing Contribute as a possible solution for editing non-database-managed content for a ASP.NET 2.0 site that uses .NET Master Pages. We'd...
    2. Limiting connections to FMS
      I was wondering if there is a notion of (to use a Java metaphor) "static" shared objects. That is objects/streams shared across FMS instances. ...
    3. Limiting Choises
      I would like to develop a Web User Control where the user can type som text eg. e-mail, number or plain text. I would like a public property...
    4. Text limiting
      I have a dynamic field that tells the news that Coldfusion outputs on a hompage. How can I limit the number of characters that news.News shows on...
    5. Limiting Old Posts
      From OE, when I am viewing the newsgroups with the "Show Replies to My Messages" option checked, there are old messages from a long time ago. How...
  3. #2

    Default Re: Limiting Output of RSS Feed

    Supposing that the items array is in descending date/time order, try
    <cfif arraylen(items) GT 10>
    <cfloop from="1" to="10" index="i">
    <cfelse>
    <cfloop from="1" to="#ArrayLen(items)#" index="i">
    </cfif>

    HTH,

    philh Guest

  4. #3

    Default Re: Limiting Output of RSS Feed

    Oops, sorry; didn't see the rss output loop at the bottom. Try

    <cfoutput query="rssItems" maxrows="10">



    philh Guest

  5. #4

    Default Re: Limiting Output of RSS Feed

    This is great. Works fine now. Thanks!
    HMOKeefe Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139