how to create hash from xml in rails?
I am trying to create hash from xml file
Hash.from_xml <<-EOX
<user>
<id>1</id>
<user-name>ryan</user-name>
</user>
EOX
when i use the above code it works fine and gives
{ :user => { :id => 1, :user_name => "ryan" } }
my problem is I have the xml part as a string
@xml ="<us开发者_如何学运维er><id>1</id><user-name>ryan</user-name></user>"
And trying to do following but its not working
Hash.from_xml <<-EOX
@xml
EOX
You don't need the mutli-line string EOX stuff:
Hash.from_xml @xml
Hash.from_xml(@xml)
#=> {"user"=>{"id"=>"1", "user_name"=>"ryan"}}
How about
Hash.from_xml @xml
The from_xml method is taking a string argument. In your working example, you are passing in a multiline string.
精彩评论