MySQL 将 XML 文件转换为 SQL 数据库的自动化方法?

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

Automated way to convert XML files to SQL database?

mysqlxmlpostgresqlschema

提问by Steve Bennett

We have a bunch of XML files, following a schema which is essentially a serialised database form:

我们有一堆 XML 文件,遵循一个本质上是序列化数据库形式的架构:

<table1>
   <column1>value</column1>
   <column2>value</column2>
</table1>
<table1>
   <column1>another value</column1>
   <column2>another value</column2>
</table1>
...

Is there a really easy way to turn that into an SQL database? Obviously I can manually construct the schema, identify all tables, fields etc, and then write a script to import it. I just wonder if there are any tools that could automate some or all of that process?

有没有一种非常简单的方法可以将其转换为 SQL 数据库?显然,我可以手动构建架构,识别所有表、字段等,然后编写脚本来导入它。我只是想知道是否有任何工具可以自动化部分或全部流程?

采纳答案by hakre

For Mysql please see the LOAD XMLSyntaxDocs.

对于 Mysql,请参阅LOAD XML语法文档

It should work without any additional XML transformation for the XML you've provided, just specify the format and define the table inside the database firsthand with matching column names:

它应该无需对您提供的 XML 进行任何额外的 XML 转换,只需指定格式并使用匹配的列名直接在数据库中定义表:

LOAD XML LOCAL INFILE 'table1.xml'
    INTO TABLE table1
    ROWS IDENTIFIED BY '<table1>';

There is also a related question:

还有一个相关的问题:

For Postgresql I do not know.

对于 Postgresql 我不知道。

回答by Well-Wisher

If there is XML file with 2 different tables then will:

如果有包含 2 个不同表的 XML 文件,则将:

LOAD XML LOCAL INFILE 'table1.xml' INTO TABLE table1 
LOAD XML LOCAL INFILE 'table1.xml' INTO TABLE table2

work

工作

回答by Preet Sangha

There is a project on CodeProjectthat makes it simple to convert an XML file to SQL Script. It uses XSLT. You could probably modify it to generate the DDL too.

CodeProject 上有一个项目,可以轻松将 XML 文件转换为 SQL 脚本。它使用 XSLT。您也可以修改它以生成 DDL。

And See this question too : Generating SQL using XML and XSLT

也请参阅此问题:使用 XML 和 XSLT 生成 SQL