Automated way to convert XML files to SQL database

2020-02-09 07:14发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 3 years ago.

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

<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?

回答1:

For Mysql please see the LOAD XML SyntaxDocs.

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:

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

There is also a related question:

  • How to import XMl file into MySQL database table using XML_LOAD(); function

For Postgresql I do not know.



回答2:

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

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



回答3:

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

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

work



回答4:

try this

http://www.ehow.com/how_6613143_convert-xml-code-sql.html

for downloading the tool http://www.xml-converter.com/