Brian J. Cardiff

Brian J. Cardiff

Build Xml files in Ruby

1 min
Nov 7 2007
Ruby
1 min
Nov 7 2007

If you have to generate xml in ruby I suggest take a look at builder gems. It has really little overhead to generate xml. Take a look to this example:

require 'Builder'
xm = Builder::XmlMarkup.new( :target => $stdout , :indent => 2 )
xm.instruct!
xm.people do | p |
  p.person(:firstname => "brian", :lastname => "cardiff") do | a |
    a.cdata!("it's me!")
  end
  p.person(:firstname => "ary", :lastname => "borenszweig")
end

will print in stdout

<people>
  <person lastname="cardiff" firstname="brian">
    <!--[CDATA[it's me!]]-->
  </person>
  <person lastname="borenszweig" firstname="ary" />
</people>

the target parameter could be a string or file (or anything with << method).