Hash has been extended to provide the ability to create a new hash from an xml string. While this is now used internally by Rails to handle XML requests, it’s also available for use as you see fit.
The basic premise is that XML is mostly a nested set of key => value pairs which is pretty much what a hash is. Population and creation of a hash from an XML structure makes perfect sense and can simplify your use of XML.
What happens when you call Hash.create_from_xml(xml) is that each xml element gets set as a key in the hash and the value of that element is stored as the value in the hash (with typecasting).
Hash.create_from_xml <<EOX
<user>
<id type="integer">1</id>
<user-name>ryan</user-name>
</user>
EOX
gets you this hash:
{ :user => { :id => 1, :user_name => "ryan" } }
Notice how the integer value of the id was actually cast as an integer. Also note how element names such as user-name get modified into the Ruby form user_name.
Collections of more than one item are handled pretty intuitively as well – being stored as an array of hashes:
hash = Hash.create_from_xml <<EOX
<users>
<user>
<id type="integer">1</id>
<user-name>ryan</user-name>
</user>
<user>
<id type="integer">2</id>
<user-name>doug</user-name>
</user>
</users>
EOX
hash[:users] #=> {:user => [ { :id => 1, :user_name => "ryan" },
{ :id => 2, :user_name => "doug" } ] }
hash[:users][:user].last #=> { :id => 2, :user_name => "doug" }
All in all just a really easy way to process XML.
tags: rails, rubyonrails, xml

hash[:users]is an array. I've corrected my example. Thanks for the heads up!