postgresql Postgres 数据到 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3559909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:15:52  来源:igfitidea点击:

Postgres Data to XML

xmlpostgresql

提问by Tian

Does anyone know any good methods for converting database entries into XML?

有谁知道将数据库条目转换为 XML 的任何好方法?

I.e. if i have a table named "Users" with fields "first_name", "age", "last_name", I'd like to convert the table to:

即,如果我有一个名为“Users”的表,其中包含字段“first_name”、“age”、“last_name”,我想将该表转换为:

<Users>
  <first_name>Papa</first_name>
  <age>50</age>
  <last_name>John</last_name>
</Users>

回答by Frank Heikens

In PostgreSQL you could it like this:

在 PostgreSQL 中,你可以这样:

SELECT table_to_xml('users', true, false, '');

Or

或者

SELECT query_to_xml('SELECT * FROM users', true, false, '');

There are other options as well, just check the manual.

还有其他选项,只需查看手册即可

回答by jigfox

This is a question independent of the DB it can be done with any DB supported by ActiveRecord.

这是一个独立于 DB 的问题,它可以通过ActiveRecord.

User.find(some_id).to_xml(:except => [:id,:created_at,:updated_at])

The :except => [:id,:created_at,:updated_at]removes the Rails default columns from the XML output.

:except => [:id,:created_at,:updated_at]去除XML输出Rails的默认列。

There is an interesting blog post about this matter: http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml

关于这个问题有一篇有趣的博客文章:http: //ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml