Generate XML with Rails December 8, 2008
Posted by Puneet Pandey in Ruby On Rails.Tags: generate xml with builder gem, generate xml with rails, how to generate xml with rails, problem in creating XML, XML with rails
trackback
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

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!
ml, :version=>”1.0″
xml.declare!
OCTYPE, :html,
UBLIC, “-//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:

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

Hi Puneet,
Thanks a lot its working great
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?
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
Hi Puneet,
Thanks a lot … it’s working very nice..