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.









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
xml creation in java
xml creation in java  HI, I need a java program to create an xml file... therez a tutorial in your site to create an xml file at http://www.roseindia.net/xml/dom/CreatXMLFile.shtmlADS_TO_REPLACE_1 but this isn't creating
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
object creation - Java Beginners
object creation  I need object creation in depth(with stack,pc registers).Any one can provide me if possible with video/audio
XML
XML  How i remove a tag from xml and update it in my xml
creation of installer - Java Magazine
creation of installer  plz tell me how can be create installer for any developed application in java?  visit the following url izpack.org.. it will helps u
exe file creation - JDBC
exe file creation   hi i have done a project in java swings.project name is format migrator.means db migrator. now my aim is create EXE FILE for my project. pls do consider
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
oXygen XML Editor
oXygen XML Editor       The simple and elegant look of the <oXygen/> XML Editor combined with the complete coverage of the XML editing features have made
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
creation button using objective c
creation button using objective c  creation button using objective c
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
XSD Creation. - WebSevices
XSD Creation.  When I try to generate xsd from java object. It is generating XSD file finely. But the problem is it isgenerating XSD file elements in alphabetical order. can u please sujest me how can we do that one. I tried
creation of a form - JSP-Servlet
creation of a form  how to create a question paper using JSP code  Hi Friend, We have taken radiobuttons in our application, you can take textarea, textfield etc. Create a table ans(ques,op1,op2,op3,op4,op5,ans
creation of database - SQL
creation of database  hi, where to and how to execute SQL queries?   Hi nanju mysql>CREATE DATABASE search; mysql> SHOW DATABASES; mysql> USE search mysql>create table Emp (fname VARCHAR(20
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  Hi...... Please tel me about that Aren't XML, SGML, and HTML all the same thing?ADS_TO_REPLACE_1 Thanks
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
creation of table using a Java swing
creation of table using a Java swing  how to create a table dynamically in Java swing
XML - XML
XML  What is specific definatio of XML? Tell us about marits and demarits of XML? use of XML? How we can use of XML?  Hi, XML... language much like HTML used to describe data. In XML, tags are not predefined
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
PDF creation in JAVA - JSP-Servlet
PDF creation in JAVA  HI! Good morning.... I want to create pdf file and i want to write something into pdf file....before creation. Upto creation i have done but how to write data into pdf. File Writer is not working
XML - XML
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java
XML - XML
XML  How to run XML program?  Hi Friend, Please visit the following links: http://www.roseindia.net/xml/ http://www.roseindia.net/xml/dom/ Hope that it will be helpful for you. Thanks
XML - XML
XML  How to convert database to XML file?   Hi Friend, Do you want to store database data into xml file ? Please clarify this. Thanks
cache creation in java - Java Beginners
cache creation in java  Hi All, I am planing to create cache in my web application. please send me the some sample code to create cache by using org.apache.oro.utils package. please help me out. Thanks, mln15584
xml - XML
xml  how to match to xml file?can you give example with java code
xml - XML
friend, XPath : It is a language for finding information in an XML document. It is used to navigate through elements and attributes in an XML document. It is a language for selecting nodes from an XML document. It is used
how to write in xml? - XML
how to write in xml?  can anybody give the code how to write in xml...(new InputStreamReader(System.in)); System.out.print("Enter XML file name...().newTransformer(); // Output Types (text/xml/html
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers... then the program contains syntax errors. XML is defined as an extensible... information,Examples and Tutorials on Xml visit to : http
xml - XML
xml  hi convert xml document to xml string.i am using below code... = stw.toString(); after i am getting xml string result, like Successxxx profile... Friend, Please visit the following link: http://www.roseindia.net/xml/reading
checkbox custom tag creation in jsf
checkbox custom tag creation in jsf  how to create check box custom tags with the following functionality: 1.there must be two checkboxes 2.when the first box is checked it must populate the names of bikes,when the second
creation an dmanipulation of trees - Java Beginners
creation an dmanipulation of trees  Hi guys? pls i want to find out a hind aboout what these terms or phrases means, -To create and manipulate a Binary Search Tree -To use the Java API's Stack in conjunction with other
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
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-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
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  

Ads