将简单的XML文件加载到Postgres [duplicate]

时间:2022-10-16 00:31:10

This question already has an answer here:

这个问题已经有了答案:

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?

有简单的方法吗?

1 个解决方案

#1


4  

Well, it can be 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.3 you get pretty good support for the json data type.
Better yet, use the jsonb data type in Postgres 9.4+.

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

#1


4  

Well, it can be 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.3 you get pretty good support for the json data type.
Better yet, use the jsonb data type in Postgres 9.4+.

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