Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Ruby on Rails and XML

Generate a Rails stub to manipulate an XML document

Daniel Wintschel (daniel@humandoing.net), Software developer, Helium Syndicate
Photo of Daniel Wintschel
Daniel Wintschel is a technology agnostic coffee drinker who loves solving problems for people and businesses. He's done a whole lot of Java programming (~7 years), and is starting to do a whole lot of Ruby programming (~1.5 years). He is the co-founder of Helium Syndicate, a company dedicated to building best of breed software solutions for small to medium sized businesses. When he's not writing software, he's likely eating, drinking coffee, or wishing he were writing software.

Summary:  You've very likely heard of Ruby on Rails. Maybe you've actually used it; perhaps it is your new programming mistress. Whatever the case, it looks like Rails is here to stay, and to everyone's benefit. Ruby plays very nicely with XML -- read further for the details.

Date:  17 Apr 2007
Level:  Intermediate PDF:  A4 and Letter (175 KB | 26 pages)Get Adobe® Reader®

Activity:  28365 views
Comments:  

Before you start

This tutorial is for a general programming crowd interested in learning the basics of setting up a skeleton Rails application and using Ruby and Rails to process XML. Beginner and intermediate programmers or people who have a little bit of exposure to Rails will likely benefit the most. It will spend a brief amount of time discussing Rails in general, and Ruby syntax as necessary, but these topics are covered in much better detail elsewhere. Please see Resources for additional information.

What is this tutorial about?

Today you will build a Rails application, discuss some basics about the way that Rails works, how it's structured and how to use it, and then you'll move on to working with XML. There are a number of ways to both generate and parse XML in Ruby, and you'll look at a few of them, including REXML (Ruby Electric XML), Builder and Hpricot (Technically Hpricot is an HTML parser -- but it's fast, and works on XML, too).


Prerequisites

The following tools are needed to follow along with this tutorial:

  • Ruby -- If you run Windows, your best bet is to download the One-Click Ruby Installer. If you use some variation of Linux or Mac OS X, you might already have Ruby installed. If not, you can download it from http://www.ruby-lang.org. The installation instructions are straightforward. Version 1.8.4 or 1.8.5 is recommended.
  • RubyGems -- Get the gems you need, and install Rails, Builder and Hpricot if you haven't already.
  • Rails -- You can install Rails through RubyGems. While not really part of this discussion, you figure all that out at http://www.rubyonrails.com/down. You'll use version 1.2.2 for this tutorial.
  • Builder -- Install through RubyGems.
  • Hpricot -- Install through RubyGems.

One of the beautiful things about Rails is how easy it is to perform object persistence and relational mapping. Since you'll only deal with XML in this tutorial, you won't actually use a database for anything.

To actually run the demo application, it's worth noting that the only thing you should have to do is to start the application. You can see how to do that in Start up the server.

Also, you'll be provided with a list of all of the files that have been explicitly created or modified aside from those that will be generated by Rails. These include:

  • app/controllers/main_controller.rb
  • views/layouts/main.rhtml
  • views/main/index.rhtml
  • public/stylesheets/reset-fonts-grids.css
  • public/stylesheets/style.css

1 of 11 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Open source, Java technology
ArticleID=208910
TutorialTitle=Ruby on Rails and XML
publish-date=04172007
author1-email=daniel@humandoing.net
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.