有没有人知道将数据库条目转换为XML的好方法?
也就是说,如果我有一个名为“Users”的表,其中包含“first_name”,“age”和“last_name”字段,我想将该表转换为:
<Users>
<first_name>Papa</first_name>
<age>50</age>
<last_name>John</last_name>
</Users>
有没有人知道将数据库条目转换为XML的好方法?
也就是说,如果我有一个名为“Users”的表,其中包含“first_name”,“age”和“last_name”字段,我想将该表转换为:
<Users>
<first_name>Papa</first_name>
<age>50</age>
<last_name>John</last_name>
</Users>
SELECT table_to_xml('users', true, false, '');
或者SELECT query_to_xml('SELECT * FROM users', true, false, '');
还有其他选项,只需要查看手册。
这是一个与数据库无关的问题,可以使用任何ActiveRecord
支持的数据库来完成。
User.find(some_id).to_xml(:except => [:id,:created_at,:updated_at])
:except => [:id,:created_at,:updated_at]
从XML输出中移除了Rails的默认列。
关于这个问题有一篇有趣的博客文章:http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml