Hash::to_xml correctly converts an array within a hash into a series of xml nodes. E.g.
h = {"songs"=>[{"title"=>"foo"}, {"title"=>"bar"}]}
puts h.to_xml
generates
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<songs>
<song>
<title>foo</title>
</song>
<song>
<title>bar</title>
</song>
</songs>
</hash>
to_xml correctly singularizes the key name to label the child nodes. This is done in conversions.rb line# 44.
However, converting this xml back to a hash via from_xml does not recognize the node songs as an array of sub-nodes:
s = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<songs>
<song>
<title>foo</title>
</song>
<song>
<title>bar</title>
</song>
</songs>
</hash>
EOS
h = Hash.from_xml(s)['hash']
puts h.inspect
gives
{"songs"=>{"song"=>[{"title"=>"foo"}, {"title"=>"bar"}]}}
which is not the same.
As a solution, either:
1. to_xml should give the option to not add the singularized subnodes, such that the first example would generate:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<songs>
<title>foo</title>
</songs>
<songs>
<title>bar</title>
</songs>
</hash>
or,
2. The from_xml method would recognize the plural-singulars and "compress" the resulting hash so that it's
{"songs"=>[{"title"=>"foo"}, {"title"=>"bar"}]}
rather than
{"songs"=>{"song"=>[{"title"=>"foo"}, {"title"=>"bar"}]}}
Jakub Roth