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 = Address.new
end

screenshot

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

–> def generate
@address = Address.new(params[:address])
@address.user_id = current_user.id
if @address.save
redirect_to :action => ‘gen_xml’
end
end
values of address are saved, let’s see our new action i.e gen_xml

–> def gen_xml
@xml = Builder::XmlMarkup.new
@addresses = Address.find(:all, :conditions => [“user_id = ?”, current_user.id])
render :layout => false
end
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”, “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”

xml.addresses{
for address in @addresses
xml.address do
xml.name(address.name)
xml.city(address.city)
end
end
}
that’s it you are done, below is the screenshot of what I have done:

screenshot-2

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

Thanks

12 responses to “Generate XML with Rails”

  1. renuka Avatar
    renuka

    Hi Puneet,

    Thanks a lot its working great 🙂

  2. kenneth Avatar
    kenneth

    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 Avatar
    Puneet Pandey

    Hi Kenneth,

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

    this is your line 1:
    xml.instruct!

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

    Let me know if it works..

    Cheers
    Puneet Pandey

  4. Ghanshyam Rathod Avatar
    Ghanshyam Rathod

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

  5. Ankit Desai Avatar
    Ankit Desai

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

    1. Puneet Pandey Avatar

      you can use Regular expression to escape such characters

  6. Maximiliano Avatar
    Maximiliano

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

    Thanks

    1. Puneet Pandey Avatar

      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 )
      end

      Thanks
      Puneet

  7. santa Avatar
    santa

    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 http://www.2dconcept.com/jquery-grid-rails-plugin 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.
    Thanks

    1. Puneet Pandey Avatar

      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
      Puneet

  8. a1aks Avatar
    a1aks

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

    Cheers

  9. Ganesh Kunwar Avatar

    Reblogged this on Ganesh Kunwar's and commented:
    Generate XML with Rails

Leave a reply to Maximiliano Cancel reply

I’m Puneet

Namaste! I’m Puneet, and this is my digital space for all things tech. I dive deep into topics like backend engineering, software architecture, and how we build robust systems. I believe in writing simple, practical articles that you can learn from and apply. Let’s build something great together!