Search code examples
ruby-on-railsxmlxml-builder

Rails XML Builder: How do I in-explicitly construct XML elements?


I am trying to use xml builder without explicit definition of elements. This is useful when the required elements are variant.

How can I accomplish something similar to the following?

 xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
 for col in [:customer, :name, :address, :city, :street, :postal_code]
   eval("xml.#{col.to_s.upcase}(#{self[col]})")
 end

This code obviously does not work if there is a ' or " in self[col]. I would also prefer not to use eval. I have already tried:

xml.send(col.to_s.upcase, self[col]

Solution

  • xml.tag!(col.to_s_upcase, self[col])