更新时间:2023-02-14 11:27:38
ActiveSupport 添加了一个 to_xml
方法到 Hash,这样你就可以非常接近你正在寻找的东西:
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 => 'joe@example.com'}
my_hash.to_xml(:root => 'customer')
最后是:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<last-name>Blow</last-name>
<first-name>Joe</first-name>
<email>joe@example.com</email>
</customer>
请注意,下划线将转换为破折号.
Note that the underscores are converted to dashes.