开发者

how to create hash from xml in rails?

开发者 https://www.devze.com 2023-03-26 08:11 出处:网络
I am trying to create hash from xml file Hash.from_xml <<-EOX <user> <id>1</id>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消