How do I convert a Ruby hash to XML?
Asked Answered
S

5

40

Here is the specific XML I ultimately need:

<?xml version="1.0" encoding="UTF-8"?>
<customer>
  <email>[email protected]</email>
  <first_name>Joe</first_name>
  <last_name>Blow</last_name>
</customer>

But say I have a controller (Ruby on Rails) that is sending the data to a method. I'd prefer to send it as a hash, like so:

:first_name => 'Joe',
:last_name => 'Blow',
:email => '[email protected]'

So, how can I convert the hash to that XML format?

Sporty answered 16/11, 2009 at 3:34 Comment(0)
A
75

ActiveSupport adds a to_xml method to Hash, so you can get pretty close to what you are looking for with this:

sudo gem install activesupport

require "active_support/core_ext"

my_hash = { :first_name => 'Joe', :last_name => 'Blow', :email => '[email protected]'}
my_hash.to_xml(:root => 'customer')

And end up with:

<?xml version="1.0" encoding="UTF-8"?>
<customer>  
   <last-name>Blow</last-name>  
   <first-name>Joe</first-name>  
   <email>[email protected]</email>
</customer>

Note that the underscores are converted to dashes.

Autophyte answered 16/11, 2009 at 3:46 Comment(4)
Ah nice, the root option was what was throwing me off. Thanks!Sporty
How does one add attributes to an XML element using this method?Pentamerous
you'll need: sudo gem install activesupport and in the code require "active_support/core_ext" to have Hash#to_xmlThelmathem
As of Rails 3.0, to_xml is no longer in ActiveSupport, but is instead added as a method on Ruby's Hash and Array classes.Exception
B
6

Gem gyoku very nice.

Gyoku.xml(:lower_camel_case => "key")    
# => "<lowerCamelCase>key</lowerCamelCase>"

Gyoku.xml({ :camel_case => "key" }, { :key_converter => :camelcase })
# => "<CamelCase>key</CamelCase>"

Gyoku.xml({ acronym_abc: "value" }, key_converter: lambda { |key| key.camelize(:lower) })
# => "<acronymABC>value</acronymABC>"

and more useful options.

Bluegrass answered 20/10, 2017 at 14:6 Comment(1)
Great post, do you know of any Gem that convert to JSON??Duster
B
3

If this data is a model, look at overriding to_xml.

Otherwise, Builder is a good option.

Beveridge answered 16/11, 2009 at 3:41 Comment(3)
Well, I've tried to_xml and it's adding a <hash> tag around my XML. ie. <hash> <customer> ... </customer> </hash>Sporty
Is your customer hash an active record object?Resinoid
Check out api.rubyonrails.org/classes/ActiveRecord/… for info on overriding how you want your XML structured.Resinoid
C
3

I would suggest a gem like XmlSimple which provides this kind of facility.

Chewy answered 16/11, 2009 at 3:41 Comment(1)
This worked for me as I am not using Rails. Here's a one-liner for hash to xml: XmlSimple.xml_out(hash, {:keeproot => true, :noescape => true})Acyclic
H
2

I did a short presentation about exactly that topic at my university a while back. Here are the slides (Interesting part starts at >= page 37)

Heavily answered 12/12, 2009 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.