data insertion from xml file to database table

data insertion from xml file to database table

Hi all,

I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me .

ThankYou

View Answers

April 20, 2011 at 5:35 PM

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


public class InsertXMLData extends HttpServlet { 
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
         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 firstNameList = firstPersonElement.getElementsByTagName("name");
                    Element firstNameElement = (Element)firstNameList.item(0);

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

                    NodeList lastNameList = firstPersonElement.getElementsByTagName("address");
                    Element lastNameElement = (Element)lastNameList.item(0);

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


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









Related Tutorials/Questions & Answers:
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions
Advertisements
Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... have developed an application to create xml file from flat file and data... an XML and data insert into the database.ADS_TO_REPLACE_2    
File insertion into oracle database
File insertion into oracle database  How to Read and Insert a file (any format) into a Oracle database
Retrieve multiple data from database into Table.....
Retrieve multiple data from database into Table.....   hi........... I want to Retrieve multiple data from database into Table but i am not able... database i have attributes pH, phosphate, ....... month, year......... so its
Select data from Table in Database
Select data from Table in Database   ... the selected data from table in database. The select query in the database retrieve... the table, from where the data is retrieve. Understand with Example
display data from a table in Access Database in a HTML page
display data from a table in Access Database in a HTML page  how to display data from a table in Access Database in a HTML page in a Java Program
Insert XML file data to database
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... to read the xml file data and save the values of an XML file to a Database
Get Data From the XML File
Get Data From the XML File       Here you will learn to retrieve data from XML file using SAX parser. We use the JAXP APIs to retrieve data from XML document . Description
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file...+",'"+email+"')"); out.println("Data is successfully inserted into database...(); in the below example. the error is "cannot convert from java.sql.Statement
dynamic retrivel of data from mysql database in table format at jsp
dynamic retrivel of data from mysql database in table format at jsp  ... the data from database and display it as table format in jsp... For example, i have... of A1 should be retrived from database and display it in the respective textbox
Display data from xml file to Swings - Swing AWT
Display data from xml file to Swings  Hi, We are Preparing a stand alone application. Where the Swings is the front end. There will be only... clicking the buttons in swings it has to display 20 record in one shot in table
Fetch Records from SQL database and convert into XML file
Fetch Records from SQL database and convert into XML file  Hi Experts... a SQL VIEW (not from original table) based on the query "SELECT * from AMAR... lets say 10 records from table / view then you need to Update the selected
database data in xml format
database data in xml format  HI, i want to display the database data in the xml format(not as xml file ) on the console using DOM. help will be appreciated. THANKS K.K
use data from database table as hyperlink value - JSP-Servlet
use data from database table as hyperlink value  I'm creating a web page that allows user to search my database. The page will display partial results of my database, then the first column of the resultset was converted
Dynamic retrieval od data from database and display it in the table at jsp
Dynamic retrieval od data from database and display it in the table at jsp ... the data from the mysql database... pls its urgent.. help me frnds....   Here is a jsp code that retrieves the data from the database and display
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database table  Here is my requirement, I want to read the records from a .xlsx file and store that records in database table. I tried like this public class
Sava data from Form to XML file using strutrs
Sava data from Form to XML file using strutrs  I'am a biginner with struts want so save data from my form in an Xml file using struts but i'm searching witout finding a solution thanks fo your help
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database for scientific research analysis. My team members sends research data in excel file
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... an XML file using Java Object then we will read that XML data as Java Object
Getting Data from XML File (Document)
Getting Data from XML File (Document)   ... from a XML file. All xml files store the data. You can add and modify the data..._TO_REPLACE_1 This program helps you in retrieving the data from a XML file
how to fetch data from mysql database table and draw a bar chart on that data using in jsp
how to fetch data from mysql database table and draw a bar chart on that data using in jsp  how to create bar chart fetch data from mysql database using in jsp.please give me a right code. yhanks in advance
Retrieve Data from the database and write into excel file
Retrieve Data from the database and write into excel file. In this section, we are going to retrieve data from the database and write into the excel file..., we have inserted the database table values in the excel sheet. Here
Retrieve data from the database and write into ppt file
Java Retrieve data from the database & write into ppt file In this section, we are going to retrieve data from the database and write into the .ppt file... * from data"); String name = ""; String address = ""; while (rs.next
Inserting Data In Database table using Statement
to insert the data in the database from our java program in the table stored... Inserting Data In Database table using Statement... Table in the database after Insertion
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Storing Data (Retrieved from a XML Document) to a File
Storing Data (Retrieved from a XML Document) to a File... to store data (retrieved from the XML document) to a specified file (with ... simple program that helps you in storing the data to a specified file in different
Displaying file from database
Displaying file from database  I have list of files in my database. I... that corresponding file from database. I have list of file id related to search. I want... = "select file_data from file where id="+id; ResultSet rs = st.executeQuery
selecting data from database
selecting data from database  how to select data from database using Dao in struts   Hi, You have to load the data from database using hibernate and send the data in a Java file object to jsp page.ADS_TO_REPLACE_1
questions from an xml file
questions from an xml file  i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one
retrieving xml document from database
retrieving xml document from database  Hi Guys, I want to retrieve the xml document stored in the database on to a jsp page using jdbc/odbc connnection.Please help me in implementing this feature. Thank You Madhu
How to Retrieve data from database in jsp
about how to fetch data from database table. We will give a simple example which will demonstrate you about fetching data from database table. Example We... tableName" for retrieving the data from database table and execute this query
How to display data fom MySQL DataBase-table in to JSP file by submitting a value in combobox.
How to display data fom MySQL DataBase-table in to JSP file by submitting... DataBase/DB Name:lokesh; Table Name:TR_list; columns:nodename,packageno,TR... to create a viewTR.jsp file in netbeans IDE, where i have to : 1.connect to DB
Export Database table to CSV File
Export Database table to CSV File In this tutorial, you will learn how to retrieve data from database and save it to CSV File. The Comma-separated values... as writing into a CSV file. Here is a database table is to be extracted:ADS
retrive data from database
retrive data from database   hi.. i made a application form. it's have attribute s.no,name,roll no and i enter a few records. now i want to view all record not in database access sheet i want to view it at any another
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... an example to insert data from file. To understand this example, we create
Store XML file into Sqlserver Database
Store XML file into Sqlserver Database  Code for storing a typical XML file into SQl server database in 1 or more tables using asp.net using C
Get date data type from table
GET DATE DATA TYPE FROM TABLE In this example , we will get "Date" data type from a table of "Mysql" database and it also display...= st.executeQuery("select name,DOB from student where name ='Ankit'");             while
Insert data in Excel File from Database using JSP
will retrieve the data from database, create an excel file and data insert... Insert data in Excel File from Database  using JSP ... developed a application to insert data  in excel file from database in JSP. We
Retrieving Data from the table using Statement
to fetch the data from the database in the table from our java program.  ... Retrieving Data from the table using Statement  ... of the PrintWriter class. We can retrieve the data from the database only and only
Upload csv or .xlsx file from JSP form to Database Table using servlet
Upload csv or .xlsx file from JSP form to Database Table using servlet  dear sir, i need the Servlet code that reads the .xlsx or CSV excel file and stores it into the oracle database table. Sir Its very Urgent I have
Display Data from Database in JSP
Display Data from Database in JSP   ... and execute query to display data from the specified table.  Before.... This is first jsp page that has a link 'show data from table', user can show
Insertion into database
Insertion into database  Hi, I need code for inserting the multiple select box values into database.Please do send me the code. Thanks for ur immediate replies its helping a lot
retrive data from database?
retrive data from database?  hellow i have a database sheet name as db1. it's contain sixty(60) sn,name ,rollno(primary key),father's name etc... from db1 and enter value in text box according there name,rollno and save
How to import data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase??
How to import data from sql server table into an excel file by creating... data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase?? There is a table in sql server having
Retrieving Data From the XML file
Retrieving Data From the XML file  ....  By this example we are going to get the XML data from the xml file in our... is to retrieve the data from the xml file. This xml file has all the information about
Retrieving All Rows from a Database Table
from a database table. You know that table contains the data in rows and columns... data from a specific database table. If any exception occurs then shows a message... Retrieving All Rows from a Database Table   
fetch data from database in javascript
fetch data from database in javascript   How to fetch data from database in JavaScript when it is a leap year
how to retrieve data from database ?????/
how to retrieve data from database ?????/  how to retrieve data from database

Ads