ruby way之处理RSS和Atom
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}" endend
require 'rss'feed = RSS::Rss.new("2.0")chan = RSS::Rss::Channel.newchan.description = "Feed Your Head"chan.link = "http://nosuchplace.org/home/"img = RSS::Rss::Channel::Image.newimg.url = "http://nosuchplace.org/images/headshot.jpg"img.title = "Y.T."img.link = chan.linkchan.image = imgfeed.channel = chani1 = RSS::Rss::Channel::Item.newi1.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.newi2.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.newi3.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 << i3puts feed
feed.channel.items = [i1,i2,i3]这样的代码,我们只能使用items[0] = i1 这样的代码.或者用我们上面代码中的使用<<方法.
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}" }
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}" }
str = feed.build_xml("rss",2.0)puts str