Johnny Spintel Johnny Spintel - 2 years ago 94
SQL Question

XML parsing to a MySQL database

I have been working on this piece of code to parse my XML file into a MySQL database. Every tutorial I have seen online parses individually like this code I've been working on. From here I would like to loop it in a way which instead of adding each field individually, if iterate it through the XML file and define how many fields need to be created inside of the table. Does anyone have any such tutorials they are aware of or possibly a short example how I could get started for this? Example I would not like to re-edit my program each time i decide to add another characteristic for a customer.

package xSQLTEST;

import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class InsertXMLData {

public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/brandonDB",
Statement st = con.createStatement();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("C:\\testXML.xml"));
System.out.println("Root element of the doc is "
+ doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("row");
for (int s = 0; s < listOfPersons.getLength(); s++) {
Node firstPersonNode = listOfPersons.item(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstPersonElement = (Element) firstPersonNode;
// -------------------------------------------------------------------------------------

NodeList CustIdList = firstPersonElement.getElementsByTagName("CustId");
Element CustIdElement = (Element) CustIdList.item(0);

NodeList textIDList = CustIdElement.getChildNodes();
String CustId = ((Node) textIDList.item(0)).getNodeValue()

NodeList NameList = firstPersonElement.getElementsByTagName("Name");
Element NameElement = (Element) NameList.item(0);

NodeList textNMList = NameElement.getChildNodes();
String Name = ((Node) textNMList.item(0)).getNodeValue().trim();

NodeList CityList = firstPersonElement.getElementsByTagName("City");
Element CityElement = (Element) CityList.item(0);

NodeList textCTList = CityElement.getChildNodes();
String City = ((Node) textCTList.item(0)).getNodeValue().trim();

int i = st.executeUpdate("insert into customer(CustId,Name,City) values('"
+ CustId
+ "','"
+ Name
+ "','"
+ City
+ "')");
System.out.println("Data is successfully inserted!");

catch (Exception err) {
System.out.println(" " + err.getMessage());

XML FILE that I am using but would like to possibly add more details about the customer in the future.

<Name>Software Solutions</Name>
<Name>Food Fantasies</Name>
<City>New York</City>


BufferedReader br = new BufferedReader(new FileReader(new File("c:\\testXML.xml")));
String line;
StringBuilder sb = new StringBuilder();

while((line=br.readLine())!= null){
String strFileContent = sb.toString();

Answer Source

Using Jsoup, you can do this:

private static void parseXml(String xml) {
    Document doc = Jsoup.parse(xml);
    StringBuilder queryBuilder;
    StringBuilder columnNames;
    StringBuilder values;

    for (Element row :"row")) {   
        // Start the query   
        queryBuilder = new StringBuilder("insert into customer(");
        columnNames = new StringBuilder();
        values = new StringBuilder();

        for (int x = 0; x < row.children().size(); x++) {

            // Append the column name and it's value 

            if (x != row.children().size() - 1) {
                // If this is not the last item, append a comma
            else {
                // Otherwise, add the closing paranthesis

        // Add the column names and values to the query
        queryBuilder.append(" values(");

        // Print the query


insert into customer(custid,name,city) values(1,Woodworks,Baltimore)
insert into customer(custid,name,city) values(2,Software Solutions,Boston)
insert into customer(custid,name,city) values(3,Food Fantasies,New York)

This allows us to add as many new columns or rows as we wish. For example, I added a few bits to your original XML, and without modification to the java code, the output was:

insert into customer(custid,name,city,age) values(1,Woodworks,Baltimore,21)
insert into customer(custid,name,city,age) values(2,Software Solutions,Boston,21)
insert into customer(custid,name,city,age) values(3,Food Fantasies,New York,21)
insert into customer(custid,name,city,age) values(4,Crazy Hat Store,London,21)


To import the XML from file, the method would look like this:

private static String loadXml(String filePath) {
    try {
        StringBuilder xml = new StringBuilder();
        Scanner scanner = new Scanner(new File(filePath)); // i.e. C:\\testXML.xml

        while (scanner.hasNextLine()) {
        return xml.toString();
    catch (FileNotFoundException e) {
    return null;

And then my main method looked like this:

public static void main(String[] args) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download