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. renuka March 2, 2009 / 6:15 AM

    Hi Puneet,

    Thanks a lot its working great 🙂

  2. kenneth March 28, 2009 / 9:18 AM

    for some reason I cannot view gen_xml.rxml it give me an error.

    XML Parsing Error: no element found
    Location: http://localhost:3000/catalog/gen_xml
    Line Number 1, Column 1:

    but I’m not sure why can you help?

  3. Puneet Pandey March 30, 2009 / 5:55 AM

    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

  4. Ghanshyam Rathod September 9, 2009 / 10:32 AM

    Hi Puneet,
    Thanks a lot … it’s working very nice..

  5. Ankit Desai August 20, 2010 / 1:17 AM

    Any idea on how to deal with tags such as this: ?

    • Puneet Pandey September 2, 2010 / 7:04 AM

      you can use Regular expression to escape such characters

  6. Maximiliano April 19, 2011 / 7:40 PM

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


    • Puneet Pandey April 28, 2011 / 4:28 PM

      you can get some help with this syntax:

      respond_to do |format|
      format.html #some action
      format.xml #this will create your xml document, access it with http://localhost:3000/users/new.xml ( an example )


  7. santa April 30, 2011 / 7:56 PM

    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.

    • Puneet Pandey May 18, 2011 / 9:44 AM

      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

  8. a1aks February 2, 2012 / 3:51 PM

    Nice bit, But could you guide me on to how to create an xml file out of rails.


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 )

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