Tom
Tom

Reputation: 34366

Parse ATOM in Ruby with custom namespaces

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)

Upvotes: 1

Views: 885

Answers (1)

elmac
elmac

Reputation: 228

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.

Upvotes: 1

Related Questions