简体   繁体   English

使用Java将xml文件数据插入数据库?

[英]Insert the xml file data into a database using java?

I wanted to insert data from a XML file to a database with Java. 我想使用Java将数据从XML文件插入数据库。 This includes creating table, followed by inserting data in that from the XML file. 这包括创建表,然后从XML文件向其中插入数据。

I'm not going to supply you with the code to do it, but give you the direction: 我不会为您提供执行此操作的代码,但会为您提供指导:

There are two parts to your task: 您的任务分为两部分:

  1. Parsing the xml - can be done by one of the many XML parsers to Java. 解析xml-可以由Java的许多XML解析器之一完成。 refer to this question . 参考这个问题

  2. Communicating with the database - can be done using JDBC , which has a nice tutorial here and another one here . 与数据库的通信-可以使用JDBC来完成, 在这里有一个不错的教程, 在这里有 另一个教程。

If this is a one time operation and/or you have LOTS of data, then you could consider bypassing the DB communication in Java (ala JDBC), and instead, you could stream the output to plain-text SQL script(s) containing valid (insert) SQL statements. 如果这是一次性操作,并且/或者您有很多数据,那么您可以考虑绕过Java(ala JDBC)中的数据库通信,而是可以将输出流式传输到包含有效内容的纯文本SQL脚本。 (插入)SQL语句。 Once you have the SQL script(s), then you could simply access your DB locally (via the command-prompt/line) and import your newly created SQL script(s). 一旦有了SQL脚本,就可以简单地在本地访问数据库(通过命令提示符/行)并导入新创建的SQL脚本。

To parse the XML, please refer to the URL provided by MByD in his point #1. 要解析XML,请在第1点中引用MByD提供的URL。

I'm not going to supply you with the code to do it, but give you the direction: 我不会为您提供执行此操作的代码,但会为您提供指导:

If this is a one time operation and/or you have LOTS of data, then you could consider bypassing the DB communication in Java (ala JDBC), and instead, you could stream the output to plain-text SQL script(s) containing valid (insert) SQL statements. 如果这是一次性操作,并且/或者您有很多数据,那么您可以考虑绕过Java(ala JDBC)中的数据库通信,而是可以将输出流式传输到包含有效内容的纯文本SQL脚本。 (插入)SQL语句。 Once you have the SQL script(s), then you could simply access your DB locally (via the command-prompt/line) and import your newly created SQL script(s). 一旦有了SQL脚本,就可以简单地在本地访问数据库(通过命令提示符/行)并导入新创建的SQL脚本。

To parse the XML, please refer to the URL provided by MByD in his point #1. 要解析XML,请在第1点中引用MByD提供的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM