title: XML parsing to a MySQL database
body: <p>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.</p>
  
  <pre><code>package xSQLTEST;
  
  import java.io.*;
  import java.sql.*;
  import org.w3c.dom.*;
  import javax.xml.parsers.*;
  
  public class InsertXMLData {
  
      public static void main(String[] args) {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/brandonDB",
                                                           "root",
                                                           "Absolute!");
              Statement st = con.createStatement();
              DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
              Document doc = docBuilder.parse(new File("C:\\testXML.xml"));
              doc.getDocumentElement().normalize();
              System.out.println("Root element of the doc is "
                                 + doc.getDocumentElement().getNodeName());
              NodeList listOfPersons = doc.getElementsByTagName("row");
              for (int s = 0; s &lt; 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()
                                                                 .trim();
  
                      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());
          }
      }
  }
  </code></pre>
  
  <p>XML FILE that I am using but would like to possibly add more details about the customer in the future.</p>
  
  <pre><code>&lt;Customers&gt;
     &lt;row&gt; 
        &lt;CustId&gt;1&lt;/CustId&gt;
        &lt;Name&gt;Woodworks&lt;/Name&gt;
        &lt;City&gt;Baltimore&lt;/City&gt;
     &lt;/row&gt;
     &lt;row&gt; 
        &lt;CustId&gt;2&lt;/CustId&gt;
        &lt;Name&gt;Software Solutions&lt;/Name&gt;
        &lt;City&gt;Boston&lt;/City&gt;
     &lt;/row&gt;
     &lt;row&gt; 
        &lt;CustId&gt;3&lt;/CustId&gt;
        &lt;Name&gt;Food Fantasies&lt;/Name&gt;
        &lt;City&gt;New York&lt;/City&gt;
     &lt;/row&gt;
  &lt;/Customers&gt;
  </code></pre>
  
  <p>BUFFER READER INSERTED AND WORKING:</p>
  
  <pre><code>BufferedReader br = new BufferedReader(new FileReader(new File("c:\\testXML.xml")));
  String line;
  StringBuilder sb = new StringBuilder();
  
  while((line=br.readLine())!= null){
      sb.append(line.trim());
  }
  String strFileContent = sb.toString();
  </code></pre>