Creation of xml

Creation of xml

Hi, I need to fetch details from my database and to create a xml file containing all those datas...My database datas are in key value pair...

AppID Label Value
12345 Applicant name XXXX
12345 Masterno       YYYY
12345 Batchno        ZZZZ
12345 Tradelicence   TTTT

On the execution of query select * from tablename where appid="+12345+".

An xml file should be generated ...My xml file should in this format XXXX YYYY ZZZZ TTTT

Can u please send me the code fulfilling all my requirements!!!!

View Answers

April 27, 2011 at 12:42 PM

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

 public class CreatXMLFile{
  public static void main(String[] args) throws Exception{
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement("APPID");
    document.appendChild(rootElement);
    Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from data where appID=12345");
           String element[]=new String[4];
           String data[]=new String[4];
           int i=0;
           while(rs.next()){
           element[i] = rs.getString("label");
           data[i]= rs.getString("value");
           i++;
           }
           for(int j=0;j<element.length;j++){
           Element em = document.createElement(element[j]);
           em.appendChild(document.createTextNode(data[j]));
           rootElement.appendChild(em);

        }   
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(new File("C:/app.xml"));
        transformer.transform(source, result);
        System.out.println("XML File has been created succesfully");
       }
    }

Before running the above code, replace your key 'Applicant name' with either Applicant_Name or with ApplicantName because xml file does not allow to leave any space in the tag.

Ads









Related Tutorials/Questions & Answers:
Creation of xml
Creation of xml  Hi, I need to fetch details from my database and to create a xml file containing all those datas...My database datas are in key... of query select * from tablename where appid="+12345+". An xml file should
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml/texttoXML.html http://www.roseindia.net/xml/dom/CreatXMLFile.shtml   
Advertisements
Creation of methods
Creation of methods  I have a only single class and its having only one method ie., main method only.... i need to develop another method that is to reduce my switching code package org.bankPackage.one; import java.util.Scanner
bean creation exception
bean creation exception  hi i am getting exception while running simple spring ioc program Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class
How to: generic array creation
How to: generic array creation  How to: generic array creation
object creation - Subversion
object creation in Java  In how many ways we can create objects in Java
XML
XML  How i remove a tag from xml and update it in my xml
xml
xml  why the content written in xml is more secure
xml
xml  validate student login using xml for library management system
xml
xml  validate student login using xml for library management system
Random Creation of password
Random Creation of password  Dear Sir I have created a form with some details in it.When clicking on the submit button it gives me a password .....Details and the password is saved in the database....The password has
Session creation and tracking
Session creation and tracking  1.Implement the information persistence across servlet destroy or servlet container start/stop. Write a servlet such that when it is stopped (either by container shutdown or servlet stop
xml
xml  what is name space,xml scema give an example for each   XML Namespaces provide a method to avoid element name conflicts.They are used for providing uniquely named elements and attributes in an XML document
XML
XML  please tell me how i remove one tag out of all similar type of tags in xml
xml
xml  how can i remove white space and next line when i copy stream to xml file
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
xml
xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0... gratis open standards. The design goals of XML emphasize simplicity, generality
XML
XML  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
xml
xml  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
Chatbox creation problem
Chatbox creation problem  i have one chat box in my web site and i assigned fixed position to that div.that is workin in mozill but it is not working in ie . thank you`print ("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
forums creation using spring with hibernate?
forums creation using spring with hibernate?  how to write mandatory fields with validations in forms using spring with hibernate? give me detailed example
ModuleNotFoundError: No module named 'music-album-creation'
ModuleNotFoundError: No module named 'music-album-creation'  Hi...: No module named 'music-album-creation' How to remove the ModuleNotFoundError: No module named 'music-album-creation' error? Thanks   Hi
ModuleNotFoundError: No module named 'music-album-creation'
ModuleNotFoundError: No module named 'music-album-creation'  Hi...: No module named 'music-album-creation' How to remove the ModuleNotFoundError: No module named 'music-album-creation' error? Thanks   Hi
ModuleNotFoundError: No module named 'music-album-creation'
ModuleNotFoundError: No module named 'music-album-creation'  Hi...: No module named 'music-album-creation' How to remove the ModuleNotFoundError: No module named 'music-album-creation' error? Thanks   Hi
ModuleNotFoundError: No module named 'xseed-minicampus-creation'
ModuleNotFoundError: No module named 'xseed-minicampus-creation'  Hi...: No module named 'xseed-minicampus-creation' How to remove the ModuleNotFoundError: No module named 'xseed-minicampus-creation' error? Thanks  
ModuleNotFoundError: No module named 'music-album-creation'
ModuleNotFoundError: No module named 'music-album-creation'  Hi...: No module named 'music-album-creation' How to remove the ModuleNotFoundError: No module named 'music-album-creation' error? Thanks   Hi
ModuleNotFoundError: No module named 'xseed-max-box-creation'
ModuleNotFoundError: No module named 'xseed-max-box-creation'  Hi...: No module named 'xseed-max-box-creation' How to remove the ModuleNotFoundError: No module named 'xseed-max-box-creation' error? Thanks   Hi
dynamic creation of multiple drop-down menus
dynamic creation of multiple drop-down menus  can anybody plz help me in coding for dynamic creation of multiple drop-down menus in a webpage using ajax/js/html/css
Creation of MultiThreads
Creation of Multiple Threads       Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread
Intranet Website creation - plz help me
Intranet Website creation - plz help me   hi.. I have assign with the intranet website creation work..right from scratch to end..And honestly I know nothing about it.My domain is totally different .. can anybody pls help me
Creation of MultiThreads
Creation of MultiThreads       Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing interface

Ads