gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Domain registration
Phone Cards
Online Universities
Prepaid Phone Card
Corporate Awards
Promos and Premiums
Imprinted Promotions
Best Price
Logo Design
Find Software
Memory
Auto Insurance Quote
Compare Prices
Disney World Tickets

 
 
Search
 



  Rethinking the Datacenter
Sponsored by HP
Today's datacenters need to increase utilization, get control over power and cooling costs, and align with business objectives. Download this eBook to learn about the challenges facing the data center in a world where digital information is growing at a torrid pace and costs are being held in check. Learn more. »
 
  Putting the Green into IT
Sponsored by HP
Electricity use in data centers is skyrocketing, sending energy bills through the roof, creating environmental concerns and generating negative publicity. "Going Green" means looking to technologies like virtualization, energy-efficient chips and racks, and implementing policies that extend beyond the data center. Learn more. »
 
  Managing the Modern Network
Sponsored by HP
In a global economy where information crosses the globe in an instant, and where Web-based applications power business, it's more important than ever to ensure your network is safe from threats and optimized to deliver the data your business needs. »
 
  Evaluating Software as a Service for Your Business
Sponsored by Webroot
Is Software as a Service just hype, or is something really going on here? See if your company can benefit as SaaS tries to change the face of the enterprise. »
 
  Is Your Disaster Recovery Plan Good Enough?
Sponsored by HP
Preparing for a disaster is more often than not part of the storage planning process, and it is one of the most difficult tasks, since it includes local hardware and software, networking equipment, and a test plan. Learn how to get disaster recovery right. »
 
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Consuming RSS Feeds with Ruby
By W. Jason Gilmore

Go to page: 1  2  Next  

Last year, noted blogger and technical evangelist Robert Scoble made headlines by explaining how he manages to read a staggering 622 RSS (Rich Site Summary) feeds each and every morning. Although attempting to digest this much information on a regular basis is probably overkill for most, it's a testament to the efficiency boost gained from subscribing to RSS feeds in lieu of navigating from one web site to the next.

But what exactly is an RSS feed, and how does one go about consuming them? In this tutorial, I'll show you how to use Ruby to retrieve and parse RSS feeds from your favorite web sites. You can use what you learn here to do something as simple as including a favorite RSS feed on your web site, or as the basis for building your own custom RSS aggregator!

RSS Internals

RSS was created almost a decade ago by Netscape for use on their My Netscape portal, which made it possible for users to customize their home pages with a variety of custom data (at the time a cutting-edge development). This XML-based format made it possible for content publishers to distribute information in a format-agnostic manner, allowing others to integrate this content into then web sites with relative ease. That is, easy if you understand RSS' XML dialect.

If you open any RSS feed within a text editor, you'll see it contains a bunch of slightly confusing tags that delimit data identified as titles, URLs, dates, creators, and descriptions, among others. For example, here's a snippet from my blog's RSS feed:

<item>
   <title>Adding Multiple Markers with YM4R</title>

   <link>http://www.wjgilmore.com/?p=38</link>
   <comments>
      http://www.wjgilmore.com/?p=38#comments
   </comments>
   <pubDate>Thu, 06 Mar 2008 14:39:29 +0000</pubDate>
   <dc:creator>wjgilmore</dc:creator>

   <guid isPermaLink="false">
      http://www.wjgilmore.com/?p=38
   </guid>
   <description><![CDATA[In this post you'll learn how to add
      multiple markers...]]></description>
   <content:encoded><![CDATA[In this post you'll learn how to
      add multiple markers...]]></content:encoded>
   <wfw:commentRss>
      http://www.wjgilmore.com/?feed=rss2&amp;p=38
   </wfw:commentRss>

</item>

Therefore, to parse and format a feed, you need to iterate over the tags found in the document, and understand the context of the content found within. Writing these sorts of capabilities from scratch can be a real chore; however, with Ruby much of the work has already been done for you!

Using Ruby to Consume RSS Feeds

RSS parsing has become so commonplace that the capability is built directly into the Ruby language. Once included in your script, the rss module will take care of all of the heavy lifting involved in parsing the feed, in the end providing you with an object from which you can access the various RSS elements. The following script will do exactly this, retrieving my blog's RSS feed, and outputting some information about the feed:

# Provides RSS parsing capabilities
require 'rss'

# Allows open to access remote files
require 'open-uri'

# What feed are we parsing?
rss_feed = "http://feeds.feedburner.com/WJasonGilmore"

# Variable for storing feed content
rss_content = ""

# Read the feed into rss_content
open(rss_feed) do |f|
   rss_content = f.read
end

# Parse the feed, dumping its contents to rss
rss = RSS::Parser.parse(rss_content, false)

# Output the feed title and website URL
puts "Title: #{rss.channel.title}"
puts "RSS URL: #{rss.channel.link}"
puts "Total entries: #{rss.items.size}"

Save this file as parserss.rb and execute it from the command line:

%>ruby parserss.rb
Title: W. Jason Gilmore
RSS URL: http://www.wjgilmore.com
Total entries: 10

Retrieving and displaying the various posts is just as easy. To see this capability in action, add the following snippet to the end of the file:

rss.items.each do |item|
   puts "<a href='#{item.link}'>#{item.title}</a>"
   puts "Published on: #{item.date}"
   puts "#{item.description}"
end

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES