Home Tutorial Java Core Insert XML file data to database

 
 

Insert XML file data to database
Posted on: September 20, 2012 at 12:00 AM
In this tutorial, you will learn how to insert the xml file data to database using dom parser.

Insert XML file data to database

In this tutorial, you will learn how to insert the xml file data to database using dom parser.

You all are aware of XML file, it is a tag based language. You can easily transfer and store its data across applications. Java has provide parsers to read the xml files efficiently and several classes to do operations in XML formatted files. Here we are going to read the xml file data and save the values of an XML file to a Database Table.

roseindia.xml file:

<roseindia>
<employee>
<name>Angelina</name>
<address>Delhi</address>
</employee>
<employee>
<name>Martina</name>
<address>Mumbai</address>
</employee>
</roseindia>

Example

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/test", "root", "root");
Statement st=con.createStatement();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("c:/roseindia.xml"));
doc.getDocumentElement().normalize();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("employee");
for(int s=0; s<listOfPersons.getLength(); s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
NodeList nameList = firstPersonElement.getElementsByTagName("name");
Element nameElement =(Element)nameList.item(0);

NodeList textFNList = nameElement.getChildNodes();
String name=((Node)textFNList.item(0)).getNodeValue().trim();

NodeList addressList = firstPersonElement.getElementsByTagName("address");
Element addressElement =(Element)addressList.item(0);

NodeList textLNList = addressElement.getChildNodes();
String address= ((Node)textLNList.item(0)).getNodeValue().trim();

int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
}
}
System.out.println("Data is successfully inserted!");
}catch (Exception err) {
System.out.println(" " + err.getMessage ());
}
}
}

Description Of Code: In this example, we have used DOM interface to read an xml file. It parses an entire XML document and load it into memory and makes an object model of it. This Object modal can be traversed to get its elements. Here the code finds its root node and fetches all the child nodes of the root node and stored them into the specific NodeList. And then get node value and stored it into respective variables. Then, we have created a database connection and save these values to database.

Output:

Related Tags for Insert XML file data to database:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.