使用自定义命名空间在 Ruby 中解析 A​​TOM

发布于 2024-09-12 10:26:26 字数 392 浏览 7 评论 0原文

我正在尝试阅读此 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)

I'm trying to read this ATOM Feed (http://ffffound.com/feed), but I'm unable to get to any of the values which are defined as part of the namespace e.g. media:content and media:thumbnail.

Do I need to make the parser aware of the namespaces?

Here's what I 've got:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2024-09-19 10:26:26

我相信你必须使用 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

我希望这能为您指明正确的方向。

I believe you would have to use libxml-ruby for that.

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

I hope that points you in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文