I have many levels of nested hash like:
{ :foo => 'bar', :foo1 => { :foo2 => 'bar2', :foo3 => 'bar3', :foo4 => { :foo5 => 'bar5' }}}
How can I convert them into an XML like this?:
<foo>bar</foo>
<foo1>
<foo2>bar2</foo2>
<foo3>bar3</foo3>
<foo4>
<foo5>bar5</foo5>
</foo4>
</foo1>
I have tried the xml.send
method, but it converts the above nested hash to:
<foo1 foo3="bar3" foo4="foo5bar5" foo2="bar2"/>
<foo>bar</foo>
How about this?
The accepted is a clean solution, but the below really does 'use' Nokogiri to construct XML from a Hash with special handling for attributes:
And the resulting XML output:
If you are using Rails you could use the built-in to_xml method.