Write to XML in ruby

Builder should probably be your first stopping point:

require 'builder'

def product_xml
  xml = Builder::XmlMarkup.new( :indent => 2 )
  xml.instruct! :xml, :encoding => "ASCII"
  xml.product do |p|
    p.name "Test"
  end
end

puts product_xml

produces this:

<?xml version="1.0" encoding="ASCII"?>
<product>
  <name>Test</name>
</product>

which looks about right to me.

Some Builder references:

  • author’s (Jim Weirich) original blog post
  • API docs
  • article at XML.com

Leave a Comment

tech