Transforming an Array of Hash objects into XML


require 'rexle'
require 'rexle-builder'

h = {fun: '123', colour: 'red', weather: [{edinburgh: 'sunny'}, {glasgow: 'rainy'}]}
a = RexleBuilder.new(h).to_a
puts Rexle.new(a).xml pretty: true

Output:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <fun>123</fun>
  <colour>red</colour>
  <weather>
    <edinburgh>sunny</edinburgh>
    <glasgow>rainy</glasgow>
  </weather>
</root>