Generate XML with Rails

Hello All,
Happy New Year in Advance, In this post of mine, I will generate XML with Buildergem. Here is how to do this:
go to terminal, in my case I m generating a new application.
–> rails myapp
go into the directory and type
–> mysql -u root -p
This will open mysql query browser in the terminal itself, create a new schema
–> create schema myapp_development;
type –> exit and get out from terminal, open your –> database.yml and change it according to that.
Now, generate a model Address by this:
–> ruby script/generate model Address
now open your addresses.rb from db/migrate folder and add following lines
–> t.string :name
t.string :city
One thing to be noted here, I m using acts_as_authenticated plugin, so I have added user_id also in that, so that It will be easy for me to generate XML for the current_user i.e the logged in user.
run –> rake db:migrate
if you have builder gem installed open up environment.rb file from config folder and add
–> require ‘builder’
after the end the ‘end’ line.
If you don’t have builder, you can easily install it by typing
–> sudo gem install builder
I think you guys are able to generate rhtml pages of it, now let’s see the controller action:
–> def new
@address =


from here my action goes to generate, see how gen_xml will store data.

–> def generate
@address =[:address])
@address.user_id =
redirect_to :action => ‘gen_xml’
values of address are saved, let’s see our new action i.e gen_xml

–> def gen_xml
@xml =
@addresses = Address.find(:all, :conditions => [“user_id = ?”,])
render :layout => false
Here Builder is your gem and it will generate XmlMarkup, here I m giving render :layout => false, coz I want to display generated XML without any CSS. Now your next step will be to create a file in your controller name gen_xml.rxml and add the below code:

–> xml.instruct! :xml, :version=>”1.0″
xml.declare! :DOCTYPE, :html, :PUBLIC, “-//W3C//DTD XHTML 1.0 Strict//EN”, “”

for address in @addresses
xml.address do
that’s it you are done, below is the screenshot of what I have done:


If you find any problem or if you have done it successfully, please leave a comment. It will give boost up my confidence level.


12 thoughts on “Generate XML with Rails

  1. Hi Kenneth,

    I’ve got this same error which you are getting do one thing and let me know..

    this is your line 1:

    I think you are giving ‘@’ symbol, so avoid that and try this with simple
    It should work

    Let me know if it works..

    Puneet Pandey

  2. I need know how to create a xml document in a action in rails. This document is for send via post to payment_site


  3. Hi puneet,

    You did a great article.
    I am new to the ruby rails world ,and it is not that easy in the beginning specially when you come from the java world but it is getting interesting now.
    I followed a jqgrid with ruby rails tutorial and I am a kind of stuck . I successfully add, edit data in the grid table but I can’t generate the xml file.
    I want to do the following:
    1. I have an xml file and I want to edit this xml file from the webinterface using ruby rails
    2.Then after generate the edited xml file

    I need your advise.

    1. Hi Santa,

      I have also used Jqgrid plugin extensively. But the feature you’re trying to implement is something new to me (but I believe that is possible). I would say learn the docs, meanwhile, If I get a chance/time to work on it, I will surely let you know.

      Best Regards

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s