Building XML files with Ruby

Is this cool or what?

    xml.rss("version" => "2.0") do
      xml.channel do
        xml.title(@feed_title)
        xml.link(@url)
        xml.description "Basecamp: Recent items"
        xml.language "en-us"
        xml.ttl "40"

        for item in @recent_items
          xml.item do
            xml.title(item_title(item))
            xml.description(item_description(item))
            xml.pubDate(item_pubDate(item))
            xml.guid(@recent_items.url(item))
            xml.link(@recent_items.url(item))
          end
        end
      end
    end

This is from the ActionPack documentation, a part of Ruby on Rails. I don’t fully understand the example (Where are the item_foo methods defined?), but this looks like a damn slick way to generate XML.

I’m very impressed by Ruby. The language’s syntax is very flexible and makes it easy to create “little languages” tailored for certain jobs, like XML generation and build files and object-relational mapping.

The obvious question: How can this be used to manipulate RDF?

This entry was posted in General. Bookmark the permalink.

1 Response to Building XML files with Ruby

  1. Andrew says:

    Although that XML-building functionality comes with Rails, it’s actually thanks to a seperate Ruby library called Builder, which lets you construct XML structures easily. See its project doc homepage at http://builder.rubyforge.org/

    You might look at Jim Weirich’s (the creator/maintainer of Builder) blog, where he frequently has little examples of it in use, like this:
    http://onestepback.org/index.cgi/Tech/Ruby/BuilderObjects.rdoc
    (BTW, that post might answer your question about where those methods are defined.)

    So, sure it could easily construct or manipulate RDF, too.

Comments are closed.