Creation of xml

Ads

 
 

Share on Google+Share on Google+

ramyajayaraman
Creation of xml
1 Answer(s)      6 years and 6 months ago
Posted in : 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!!!!

Ads
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
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.shtml but this isn't creating a file, its just
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
XML
XML  How i remove a tag from xml and update it in my xml
How to: generic array creation
How to: generic array creation  How to: generic array creation
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
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  Hi...... Please tel me about that Aren't XML, SGML, and HTML all the same thing? Thanks
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
object creation - Subversion
object creation in Java  In how many ways we can create objects in Java
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
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
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
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
creation button using objective c
creation button using objective c  creation button using objective c
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
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
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java

Ads

 
Advertisement null

Ads