How to Generate XML files from the data in DB tables?

How to Generate XML files from the data in DB tables?

HI Experts, I have a doubt.

I have a table say Pest.

My task is to generate XML file with all the fields in table and store it to database.Please help me.I am new to XML.

Is there any tools to generate xml files?

View Answers

September 27, 2011 at 4:59 PM

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreateXML{
public static void createXmlFile(Document doc,String name,String address,String contactNo,String email) throws Exception {
Element root = doc.createElement("Employee");
doc.appendChild(root);
Element element1 = doc.createElement("Emp_Name");
root.appendChild(element1);
Text text1 = doc.createTextNode(name);
element1.appendChild(text1);

Element element2 = doc.createElement("Emp_Address");
root.appendChild(element2);
Text text2 = doc.createTextNode(address);
element2.appendChild(text2);

Element element3 = doc.createElement("Emp_ContactNo");
root.appendChild(element3);
Text text3 = doc.createTextNode(contactNo);
element3.appendChild(text3);

Element element4 = doc.createElement("Email");
root.appendChild(element4);
Text text4 = doc.createTextNode(email);
element4.appendChild(text4);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();

File file = new File("c:/employee.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
bw.write(xmlString);
bw.flush();
bw.close();
}
public static void main(String args[])throws Exception throws Exception{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();

Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "satya", "satya" );
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from employee where id=1");
        String name=" ",address=" ",contactNo=" ",email=" ";
        if(rs.next()){
             name=rs.getString("name");
             address=rs.getString("address");
            contactNo=rs.getString("contactNo");
             email=rs.getString("email");
        }
createXmlFile(doc,name,address,contactNo,email);
System.out.println("Xml File Created Successfully");
}
}









Related Tutorials/Questions & Answers:
How to Generate XML files from the data in DB tables?
How to generate XML from XSD?
Advertisements
how to search data in xml files using php
ModuleNotFoundError: No module named 'generate-files-data'
How to access data yearly from DB in C# ?
how to insert, retrieve data from,to db(code)....
how to insert, retrieve data from,to db(code)....
How to insert data from a combobox and textbox values into DB using JSP?
How to get data from DB in to Text box by using Jsp & Ajax
where is my sqlite data files(.db) will be saved?
Getting Data from XML File (Document)
Generate xml
How to generate xml file using xpath
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
how to generate the pdf report from jsp
Read data from Excel and insert in to DB and export data from DB to Excel
data insertion from xml file to database table
pdf generate from jsp
how to read and write xml files in javascript
Retrieve data from xml using servlets
how to generate reports from oracle database using jsp and ajax code
how to update xml from java - XML
Get Data From the XML File
how to generate the pdf file with scroolbar from jsp age - JSP-Servlet
JAXB Create XML File And Get Data From XML
how to create xml schema from xml doc using java
How to create XML from Swings
how to write java data - XML
how to featch data form db?
How to access the Title tag from xml to jsp
Reading multiple xml files
How to create one xml file from existing xml file's body?
How to access the following tag from xml to jsp
Retrieving Data From the XML file
How to read value from xml using java?
Sava data from Form to XML file using strutrs
how to read values from java in xml?
How to pretty print XML from Java?
creating index for xml files - XML
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
How to values from xml using java?
Data needs to be gathered in XML file from the database (MySql) using JSP
Display data from xml file to Swings - Swing AWT
Storing Data (Retrieved from a XML Document) to a File
How Compare 2 xml files with JDOM - Java Beginners
Generate Image Files
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database? - XML
how to retrieve data from database ?????/

Ads