I wanted to insert data from a XML file to a database with Java. This includes creating table, followed by inserting data in that from the XML file.
-
Which Database are you using? – Ankit May 11 '11 at 04:36
-
2You need to clarify if you just want to store the XML string in the database, or extract data from the XML and populate a table row (or rows). If you want the first option the answer is simple. If it's the second you must provide much more information. – Jim Garrison May 11 '11 at 04:36
3 Answers
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:
Parsing the xml - can be done by one of the many XML parsers to Java. refer to this question.
Communicating with the database - can be done using JDBC, which has a nice tutorial here and another one here.
-
Well, depending on what the OP means by "insert XML content into a database", there might not be any need to parse the XML. Maybe just sticking the actual bytes of the XML into the DB is what is desired? That aside, totally agree with your answer. – QuantumMechanic May 11 '11 at 04:32
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. 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).
To parse the XML, please refer to the URL provided by MByD in his point #1.
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. 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).
To parse the XML, please refer to the URL provided by MByD in his point #1.

- 87,323
- 22
- 191
- 272