postgresql 将简单的 XML 文件加载到 Postgres

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22771503/
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-10-21 01:23:13  来源:igfitidea点击:

Load simple XML file into Postgres

xmlpostgresqlparsingload

提问by ADJ

I have a file with a simple xml structure that I want to load into a Postgres table.

我有一个具有简单 xml 结构的文件,我想将其加载到 Postgres 表中。

<rows>
<field name="id">1</field>
<field name="age">75-84</field>
<field name="gndr">F</field>
<field name="inc">32000-47999</field>
</rows>

Is there an easy way of doing this?

有没有简单的方法来做到这一点?

回答by Erwin Brandstetter

Well, it canbe done. I wrote a complete function for the same purpose a while back:

那么,它可以完成。不久前,我出于同样的目的编写了一个完整的函数:

I use pg_read_file()to read in the whole file into a variable in plpgsql and proceed from there. This carries a few restrictions:

我使用pg_read_file()将整个文件读入 plpgsql 中的变量并从那里继续。这有一些限制:

Only files within the database cluster directory and the log_directory can be accessed. Use a relative path for files in the cluster directory, and a path matching the log_directory configuration setting for log files. Use of these functions is restricted to superusers.

只能访问数据库集群目录和 log_directory 中的文件。对集群目录中的文件使用相对路径,并使用与日志文件的 log_directory 配置设置匹配的路径。这些功能的使用仅限于超级用户。

But I listed alternatives.

但我列出了替代方案。

If you can convert the XML file to JSON it gets simpler. There are a bunch of tools for that available.

如果您可以将 XML 文件转换为 JSON,它会变得更简单。有很多可用的工具

Starting with Postgres 9.3you get pretty good support for the jsondata type.
Better yet, use the jsonbdata type in Postgres 9.4+.

从 Postgres 9.3开始,您将获得对json数据类型的良好支持。
更好的是,使用jsonbPostgres 9.4+ 中的数据类型。