Sunday, 15 September 2013

convert XML to ruby hash with attributes -



convert XML to ruby hash with attributes -

objective :

convert xml ruby hash , node , attributes values

what tried :

xml = '<test id="appears"> <comment id="doesnt appear"> worked </comment> <comment> see! </comment> <comment /> </test>' hash = hash.from_xml(xml)

now hash

#=>{"test"=>{"id"=>"appears", "comment"=>["it worked", "see!", nil]}}

notice how id attribute on first comment element doesn't appear.

how resolve ?

i found solution here gem

gem 'cobravsmongoose', '~> 0.0.2'

try ,

hash =cobravsmongoose.xml_to_hash(xml)

here result :

{"test"=>{"@id"=>"appears", "comment"=>[{"@id"=>"doesnt appear", "$"=>"it worked"}, {"$"=>"see!"}, {}]}}

ruby-on-rails ruby xml

No comments:

Post a Comment