views:

245

answers:

3

How do I create a XML file from a form in ruby on rails. The form elements should be converted into xml file and stored in my system at a specific location. I am a newbie. so please help me out with some codes.

Thanks in advance.

A: 

woohoo, vague questions :)

Try the newbie approach to xml

xml = "<outerNode><innerNode>#{params[:someparam]}</innerNode></outerNode>"

check out FileUtils for how to save strings to files

Luke Schafer
A: 

i know how to use file utils to save the file.. the prob is how do i generate the and the from the form elements? shoud i use the Builder class?

Nave
Add a comment to Luke's answer instead of making a new answer.
musicfreak
And welcome to Stack Overflow. :)
musicfreak
A: 

You could do something like this:

#!/usr/bin/ruby

require 'builder'

favorites = {
  'candy' => 'Neccos', 'novel' => 'Empire of the Sun', 'holiday' => 'Easter'
}

xml = Builder::XmlMarkup.new( :target => $stdout, :indent => 2 )

xml.instruct! :xml, :version => "1.1", :encoding => "US-ASCII"

xml.favorites do 
 favorites.each do | name, choice |
  xml.favorite( choice, :item => name )
 end
end

http://www.xml.com/lpt/a/1637

kristian nissen