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.
-
1Which Database are you using?Ankit– Ankit2011-05-11 04:36:23 +00:00Commented May 11, 2011 at 4:36
-
3You 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– Jim Garrison2011-05-11 04:36:34 +00:00Commented May 11, 2011 at 4: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.
1 Comment
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.
Comments
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.