【问题标题】:Parse ATOM in Ruby with custom namespaces使用自定义命名空间在 Ruby 中解析 ATOM
【发布时间】:2010-07-31 12:54:26
【问题描述】:
我正在尝试阅读此 ATOM Feed (http://ffffound.com/feed),但无法获取任何定义为命名空间一部分的值,例如媒体:内容和媒体:缩略图。
我需要让解析器知道命名空间吗?
这是我得到的:
require 'rss/2.0'
require 'open-uri'
source = "http://ffffound.com/feed"
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
【问题讨论】:
标签:
ruby
parsing
namespaces
rss
atom-feed
【解决方案1】:
我相信您必须为此使用 libxml-ruby。
gem 'libxml-ruby', '>= 0.8.3'
require 'xml'
xml = open("http://ffffound.com/feed")
parser = XML::Parser.string(xml, :options =>XML::Parser::Options::RECOVER)
doc = parser.parse
doc.find("channel").first.find("items").each do |item|
puts item.find("media:content").first
#and just guessing you want that url thingy
puts item.find("media:content").first.attributes.get_attribute("url").value
end
我希望这会为您指明正确的方向。