19

I using some service that returns xml:

response = HTTParty.post(service_url)
response.parsed_response 
=> "\n\t<Result>\n<success>\ntrue\n</success>\n</Result>"

I need to convert this string to hash. Something like this:

response.parsed_response.to_hash
=> {:result => { :success => true } }

Which way to do this?

1

5 Answers 5

42

The built-in from_xml Rails Hash method will do precisely what you want. In order to get your response.parsed_response correctly mapped to a hash, you'll need to gsub() out the newlines:

hash = Hash.from_xml(response.parsed_response.gsub("\n", "")) 
hash #=> {"Result"=>{"success"=>"true"}}

In the context of parsing a hash in Rails, objects of String type are not substantively different than those of Symbol from a general programming perspective. However, you can apply the Rails symbolize_keys method to the output:

symbolized_hash = hash.symbolize_keys
#=> {:Result=>{"success"=>"true"}} 

As you can see, symbolize_keys doesn't operate on any nested hashes, but you could potentially iterate through inner hashes and apply symbolize_keys.

The final piece of the puzzle is to convert the string "true" to the boolean true. AFAIK, there's no way to do this on your hash in place, but if you're iterating/operating on it, you could potentially implement a solution like the one suggested in this post:

def to_boolean(str)
     return true if str == "true"
     return false if str == "false"
     return nil
end

Basically, when you reach the inner key-value pair, you'd apply to_boolean() to the value, which is currently set to "true". In your example, the return value is the boolean true.

1
12

Use nokogiri to parse XML response to ruby hash. It's pretty fast.

require 'active_support/core_ext/hash'  #from_xml 
require 'nokogiri'

doc = Nokogiri::XML(response_body)
Hash.from_xml(doc.to_s)
5

You can try this below:

require 'active_support/core_ext/hash/conversions'  
str = "\n\t<Result>\n<success>\ntrue\n</success>\n</Result>".gsub("\n", "").downcase

Hash.from_xml(str)
# => {"result"=>{"success"=>"true"}}
1
  • Thanks for including the require to use in ruby outside rails! Commented May 30, 2014 at 11:47
3

Use gem Nokogir

doc = Nokogiri::XML(xml_string)

data = doc.xpath("//Result").map do |result|
  [
    result.at("success").content
  ]
end

These tutorials may help you.

1
  • hope it will not give exact hash as i got #<Nokogiri::XML::Document:0x16e3790 name="document" children=[#<Nokogiri::XML::Element:0x16e32e0 name="result" children=[#<Nokogiri::XML::Element:0x16e30ec name="success" children=[#<Nokogiri::XML::Text:0x16e2de0 "true">]>]>]> Commented Jun 13, 2013 at 5:20
1

I found a gem which does exactly that:

gem 'actionpack-xml_parser'

Basically, each node represents a key. With the following XML:

<person><name>David</name></person>

The resulting parameters will be:

{"person" => {"name" => "David"}}

https://github.com/eileencodes/actionpack-xml_parser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.