What's New in Edge Rails: Create a Hash from XML

Posted by ryan
at 6:13 AM on Tuesday, June 27, 2006

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.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).

For instance, this:

Hash.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.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

Comments

Leave a response

  1. Roman LE NEGRATEJune 18, 2006 @ 02:17 PM
    Hello Ryan, I did not try Hash#create_from_xml but think you made a mistake here: hash[:users] #=> { :user => { :id => 1, :user_name => "ryan" }, :user => { :id => 2, :user_name => "doug" } } The hash cannot have two values with the same key. Isn't hash[:users] an Array ?
  2. Seth Thomas RasmussenJune 18, 2006 @ 02:17 PM
    This wouldn't be useful if the order of your markup were relevant, though, correct?
  3. Ryan DaigleJune 18, 2006 @ 02:19 PM
    You're right Roman - hash[:users] is an array. I've corrected my example. Thanks for the heads up!
  4. Ryan DaigleJune 18, 2006 @ 04:46 PM
    Seth, well identical elements' orderings are retained. So the first "user" XML element becomes the first :user item in the :user array. I may be missing your intent though...
  5. Seth Thomas RasmussenJune 18, 2006 @ 04:46 PM
    I think I see now.. you're only ever dealing with a one-pair hash, all nested in arrays, so yes, the order of elements is always kept.
  6. Wayne RobinsonJune 30, 2006 @ 07:21 AM
    I wrote some code back in May (and blogged about it) that takes this one step further and extends the ActiveRecord model to allow you to pass it XML and have it create an ActiveRecord model including associations. You can see my blog article at http://www.wayne-robinson.com/journal/2006/5/1/ruby-on-rails-activerecordbuild_from_xml-function.html
  7. Zak MandhroJune 30, 2006 @ 07:21 AM
    Good idea. This will take care of majority of the cases. Alternatively, you can use fully specified Ruby classes to represent XML using the ROMXL open-source library. http://roxml.rubyforge.org Thanks for the intro! - Zak
  8. Dominic OrchardAugust 25, 2006 @ 02:00 AM
    In reference to Wayne's code from May I have been working on a similar thing too and have enhanced Wayne's code:- http://riftor.g615.co.uk/content.php?view=50&type=1