der kl@mmeraffe | ruby. java. development.

a developers guide to the world of code

Tuesday, July 25, 2006

Ruby and XML

Ruby has builtin-support for XML via the rexml-module. This module supports various types of XML-parsing (e.g. building trees, event based streaming,...). We'll try the DOM-like-tree, but don't be afraid - its absolutely not as horrible as working with the DOM api under java.

Now, we want to parse the titles from a given weblog. We gotta use 3 modules for that

require 'rexml/document'
require 'net/http'
require 'uri'

Just that we don't have to type REXML before every command, we include that class

include REXML

Reading data over http is pretty straightforward in ruby

url = 'http://danslog.blogspot.com/atom.xml'
data = Net::HTTP.get(URI.parse(url))


Thats all. But we want to parse, unmarshall and whatever the given xml data

doc = Document.new(data)

The variable doc now contains the DOM-like-tree. Lets do an each over the titles in the feed. You can use xpath for that (imho the best xml spec out there)

doc.root.elements.each("entry/title") do |element|
  p element.text
end

That should output something like this:

"Groovy and SOAP"
"Pattern: Good Citizen"
"Ruby on Rails"
"News: AJAX-Framework als Apache-Projekt vorgeschlagen"
"Picocontainer"

Of course, streaming rss-feeds is even easier with ruby streaming apis for, well, rss-feeds, but i wanted to show how easy it is to work with xml under ruby.

Here is the whole class:

require 'rexml/document'
require 'net/http'
require 'uri'

include REXML

class RSSTitleCheck
  def initialize
    url = 'http://danslog.blogspot.com/atom.xml'
    data = Net::HTTP.get(URI.parse(url))
    doc = Document.new(data)
    doc.root.elements.each("entry/title") do |element|
      p element.text
    end
  end
end

RSSTitleCheck.new


You can find more info about REXML and HTTP-Access for Ruby here:

rexml intro @ xml.com
rexml tutorial @ germane-software.com
networking libs @ rubycentral.com/book

add to del.icio.us | submit to digg | submit to reddit

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home