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" endendputs product_xml
produces this:
<?xml version="1.0" encoding="ASCII"?><product> <name>Test</name></product>
which looks about right to me.
Some Builder references:
Simply withNokogiri::XML::Builder
require 'nokogiri'builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| xml.root { xml.products { xml.widget { xml.id_ "10" xml.name "Awesome widget" } } }endputs builder.to_xml
Will output:
<?xml version="1.0" encoding="UTF-8"?><root> <products> <widget> <id>10</id> <name>Awesome widget</name> </widget> </products></root>