Readers: 49 | Updated: 01-31

ruby way之处理RSS和Atom

Translate Into:

网站: JavaEye  作者: simohayha  链接:http://simohayha.javaeye.com/blog/160622  发表时间: 2008年01月31日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

1 rss 标准库

RSS 是基于xml的,因此你能简单的将它作为一个xml来进行解析.可是,事实上他有一个更高级别的专有的解析器来解析他.此外RSS的标准也很混乱。

他有很多不便,就是标准的版本的不兼容,RSS有0.9, 1.0, 和2.0的版本.RSS的版本,就像制造热狗,就是一些除非你一定要知道,否则你不想要知道的细节.

ruby有一个标准RSS库,它包含0.9,1.0和2.0版本标准.不同的版本之间尽可能做到了无缝处理.如果你没有指定版本,这个库他会自己尽可能地检测版本.

看下面的例子,这里我们是从http://marsdrive.com 得到feed:

require 'rss'
require 'open-uri'

URL = "http://www.marstoday.com/rss/mars.xml"
open(URL) do |h|
  resp = h.read
  result = RSS::Parser.parse(resp,false)
  puts "Channel: #{result.channel.title}"
  result.items.each_with_index do |item,i|
    i += 1
    puts "#{i}  #{item.title}"
  end
end


这个程序很简单,一直要看一下mars.xml的结构就很清晰了.

下面我们来创建一个RSS Feed

require 'rss'

feed = RSS::Rss.new("2.0")

chan = RSS::Rss::Channel.new
chan.description = "Feed Your Head"
chan.link = "http://nosuchplace.org/home/"

img = RSS::Rss::Channel::Image.new
img.url = "http://nosuchplace.org/images/headshot.jpg"
img.title = "Y.T."
img.link = chan.link

chan.image = img
feed.channel = chan

i1 = RSS::Rss::Channel::Item.new
i1.title = "Once again, here we are"
i1.link = "http://nosuchplace.org/articles/once_again/"
i1.description = "Don't you feel more like you do now than usual?"

i2 = RSS::Rss::Channel::Item.new
i2.title = "So long, and thanks for all the fiche"
i2.link = "http://nosuchplace.org/articles/so_long_and_thanks/"
i2.description = "I really miss the days of microfilm..."

i3 = RSS::Rss::Channel::Item.new
i3.title = "One hand clapping"
i3.link = "http://nosuchplace.org/articles/one_hand_clapping/"
i3.description = "Yesterday I went to an amputee convention..."

feed.channel.items << i1 << i2 << i3

puts feed


这个代码也很简单,就是先创建了一个空的RSS 2.0 feed ,然后给他的一些节点开始赋值.

这里要注意我们想要把一些Item对象付给feed.channel.items时,我们不能使用
feed.channel.items = [i1,i2,i3]
这样的代码,我们只能使用items[0] = i1 这样的代码.或者用我们上面代码中的使用<<方法.

很多人可能更喜欢用Atom,比起RSS,可是rss库不支持Atom,但是非标准的feedtools 库支持Atom.我们下面就要介绍它.

2 feedtools Library

feedtools他以无缝的方式工作在Atom和RSS中,它存储所有的feeds作为一个共有的内部的格式.并且它有它自己的url处理代码,因此你不需要显示的使用net/http 或者open-uri.

下面这段代码是用feedtools来处理前面的那个例子:

require "feed_tools"

URL = "http://www.marstoday.com/rss/mars.xml"
feed = FeedTools::Feed.open(URL)
puts "Description: #{feed.title}\n"

feed.entries.each_with_index {|x,i| puts "#{i+1} #{x.title}" }
 


这段代码比起上面的那段代码更为简洁和清晰.可以看到这里并没有显式的channel 方法,可是你却可以直接在feed对象上调用title等方法,这是因为一个feed就是一个channel.

下面的是存取Atom的例子:

require "feed_tools"

URL = "http://www.atomenabled.org/atom.xml"
feed = FeedTools::Feed.open(URL)
puts "Description: #{feed.title}\n"

feed.entries.each_with_index {|x,i| puts "#{i+1}  #{x.title}" }


注意,对比一下这两个代码,不同点只有一个,那就是URL的不同,这就说明我们处理feeds时,不需要知道他被存储为哪种格式.

现在让我们加下面的代码到上面的例子中:

str = feed.build_xml("rss",2.0)
puts str


我们所做的就是将Atom feed 转换为RSS2.0的feed.你也可以转换成0.9或者1.0版本.因此我们能读一个RSS的feed然后制造出一个Atom的feed.这个就是这个库的强大之处.

这里要强调一下,我们上面的例子都只是一个小测试,如果你要真正做一个应用程序的话,你要注意处理你的缓存.
本文的讨论也很精彩,浏览讨论>>


JavaEye推荐
中国领先的电子商务网站-淘宝网招贤纳士,诚聘Java工程师



From The Blogs

iTech News Net

04-20
MWg Atom V PDA Phone
MWg (Mobile Wireless Group), founded by ex-O2 developers, presents their first product, Atom V PDA Phone. The Atom V is powered by an Intel XScale PXA270 520MHz processor, 256MB ROM, and 64MB RAM. It ... 查看全文

Pro Blog Design

04-11
60 Great RSS Icons for Your Blog
The standard RSS icon is too common. Everyone has seen it. Make your feed subscription more appealing by using one of the unique icons below. 查看全文

iTech News Net

04-10
Imation Atom Flash Drive
Imation ships its Atom flash drive that is as small as a paperclip. It is available in 1GB, 2GB, 4GB, and 8GB of capacity. It has aluminum case and offers password protection. [Electronista] Related P... 查看全文

Product Reviews Net

04-02
Video of Lenovo Ideapad U8 MID complete with Intel’s Atom processor
I think that I have just fallen in love; I know I am sick, but come on just look how sexy and sleek the new Lenovo Ideapad U8 MID looks. To help you get an idea at just how great it looks, just check ... 查看全文

MakeUseOf.com

03-22
RSS Feed Button Generator
Want to create own RSS subscription button for your blog? Check out RSS Feed Button Generator. This little tool lets you design your own RSS subscription button and download it as an image. Simply sel... 查看全文

2007
Pluck
http://www.pluck.com/We build social media solutions that transform how everyday people and publishers discover, create and distribute information online. Providing RSS, blogging, syndication and soci... 查看全文

KillerStartups.com - all

03-11
Prixfeed.com - Delivering RSS Feeds of Amazon Products
What it doesIf the price were right, you’d buy it.As it so often happens, Amazon is just busting with goodies that you would very much like to see in your own hands; however, as you can’t spend all yo... 查看全文

ReadWriteWeb

03-05
6 Ways to Filter Your RSS Feeds
RSS is easily one of the best things to happen to web publishing in the past 10 years.It allows users to easily keep track of news from multiple web sites because updates are delivered directly to the... 查看全文

Download Squad

03-19
Eluma: Desktop RSS reader, bookmark manager, and more
Filed under: Internet, Windows, Beta, web 2.0 Eluma is a new Windows application for viewing and managing web content. Basically, it's an RSS reader and bookmark manager bundled together. When you fir... 查看全文

Internet Observation

2007
Instant Messenger BOT development Community
Instant Messenger BOT development Communitywww.improvcenter.com 发展社区允许即时消息BOT's进行廉价创造并在社区中进行交流和推广。来自... 查看全文
More Articles