Instead of spending hours trying to write something custom to convert your hash into xml or vice versa, you can do it in one line. If you are already in a Rails environment, you are good to go, but if using ruby, include the ActiveSupport gem:
Now that you have, we can convert a xml to a hash:
hash = Hash.from_xml(xml_stream)
And a hash to xml is stupid simple:
xml = hash.to_xml
There you have it, simple xml to hash and hash to xml!