Home Answers Viewqa XML storing data in xml

 
 


kskumar
storing data in xml
2 Answer(s)      2 years and 10 months ago
Posted in : XML

Can u plz help me how to store data in xml using java
View Answers

July 29, 2010 at 11:10 AM


Hi Friend,

Try the following code:

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{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Scanner input=new Scanner(System.in);
System.out.print("Enter Employee Name:");
String name=input.nextLine();
System.out.print("Enter Address:");
String address=input.nextLine();
System.out.print("Enter Contact No:");
String contactNo=input.nextLine();
System.out.print("Enter Email:");
String email=input.nextLine();
createXmlFile(doc,name,address,contactNo,email);
System.out.println("Xml File Created Successfully");
}
}

Thanks

September 27, 2011 at 4:58 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 Pages:
storing data in xml - XML
storing data in xml  Can u plz help me how to store data in xml using java  Hi Friend, Try the following code: import java.io....,name,address,contactNo,email); System.out.println("Xml File Created
storing xml into database - XML
storing xml into database   hi i have an xml file it contains... .......with it i know how to persist a simple xml file into data base but i m finding some difficulty to start with i m sending u the xml file
complex xml parsing and storing in database - XML
complex xml parsing and storing in database  Hi Experts ,i want to parse my xml document and store it in mysql database. This is my code. How to parse this complex data. EDI_DC40 800 0000000000557748
Produces XML file but format not correct for storing data using JSP and XML
Produces XML file but format not correct for storing data using JSP and XML  hii I have created a project using JSP and XML as database to store data entered by user in XML file ,It stores data entered in XML file
sorting and storing data
sorting and storing data   sorting and storing data in UITableView
iphone storing and fetching data
iphone storing and fetching data  How to store and fetch data in an iphone application
storing data into flat files
storing data into flat files  how can i retrive data from database and store data in flat files   Hi Friend, Try the following code: import java.io.*; import java.sql.*; import java.util.*; class StoreDataIntoFile
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 ... that helps you in storing the data to a specified file in different format. After
Storing and Reading data
Storing and Reading data   Hello, I'm developing a GUI application as part of an assignment but stuck on how my program stores and reads the data... that limits the data type and the character size that can be entered
to fetch data from ms word and storing into database
to fetch data from ms word and storing into database  i want to know how to fetch datafields from ms word and storing into database??? please answer soon .its urgent
About XML - XML
About XML  What are possible ways XML used in j2ee WebApplication.Like storing data in to database and for data transfer .Apart from this any uses.... Thanks  Hi, See the different uses of XML at http://roseindia.net
Storing properties in XML file
Storing properties in XML file       This Example shows you how Store properties in a new XML File. JAXP (Java API for XML Processing) is an interface which provides parsing
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
xml
, sub-elements and attributes needed in a xml document. It defines the data...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
xml
(APIs) have been developed for software developers to use to process XML data...xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0
What is XML?
mainly to store and transport data XML is also used by the applications... application. It's a structured document for storing and transporting the data... booking application may send the data in xml format to the credit card processing
XML data binding
XML data binding   hello, What is XML data binding?   hii, XML data binding refers to the process of representing the information in an XML document as an object in computer memory
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
Data read. - XML
Data read.  How to store or read data in XML from Struts.Plz give me example. Thanx
XML - XML
language much like HTML used to describe data. In XML, tags are not predefined... Definition (DTD) , XML Schema to describe the data. Hence it is self-descriptive too...XML  What is specific definatio of XML? Tell us about marits
Comparing XML with HTML
than storing data. It also eliminates the need to change in html when xml... below: XML is designed to carry the data while html is to display the data and focus on how the data looks. XML does not do anything
saving data in xml
saving data in xml  Hi, I have an xml file with spring map, in that i have parent child nodes. I have jsp form in that i have put same parent child elements. when i submit jsp form I want to append same data into xml file
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
xml displaying a drives data.....
xml displaying a drives data.....  Hi all, I need a solution for displaying content of a drive(Ex: c , d , e ) in the browser using the XML... the XML. Thanks in advance.......   Hi, You can't access the your drive
Why XML?, Why XML is used for?
work as the data file (xml) for storing the names and location... the structured data into XML files. There are various uses of the XML files. Why XML is used? We developers uses the XML files for following purposes: Storing
data retrivel code - XML
data retrivel code  Can someone help me in retriving data from MySql database into an XML file using java.  Hi Shruti, Please visit.../servlets/login-Xml-servlet.shtml I hope this will help you a lot. Thanks
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop... into data(language) values('"+selectedValue+"')"); out.println("Data... lyk this. but it storing null value in the database. :\ what to do
data retrival and presentation - XML
data retrival and presentation  I want to present the data which is retrieved from xml file. Which contains 1000 records.so i want to present them...://www.roseindia.net/jsp/data-grid.shtml http://www.roseindia.net/jsp/parsing-xml.shtml
Java Xml Data Store
Java Xml Data Store  I have to do this project and i'm finding it so... be followed up and/or purchased. You will need to store the data in a local binary or XML file. As a good Java programmer you will use correct Object-Oriented
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers?   Hi friend, PARSER - It separates data into more easily understood... then the program contains syntax errors. XML is defined as an extensible
retrievin xml data
retrievin xml data  If i need to retrieve some node elements from xml...(0); } } } For the above code, we have used following xml file: <?xml version = "1.0" ?> <EmployeeDetail> <Employee> <Emp_Id>
how to write java data - XML
how to write java data  how to write data to xml file  Hi friend, Read for more information, http://www.roseindia.net/xml/dom/ Thanks
xml or database - XML
xml or database  If I implement some web applications, which is a better way to retrieve and parse xml file directly using DOM or storing those xml files in Database and retrieve. My xml files will be about 100 - 200 and each
project in JSP and XML(to store data)
project in JSP and XML(to store data)  I need to complete an assignment in JSP and using XML as Database ,Please help me to complete this assignment.Ill be very very thankful to you,i am new to JSP.Please provide full code
xml code - XML
xml code  I Read xml is used to store and transfer data can you explain it with sample code
storing details in database on clicking submit button - JSP-Servlet
storing details in database on clicking submit button  I am using JSP in NetBeans and have developed an application form which has fileds naming......Tell me where an what code should I write to store the values in data base table
Retrieving XML Data Using GWT
Retrieving XML Data Using GWT       This Example Describes the way of retrieving XML file Data from...;static final String XML_LABEL_STYLE = "xmlLabel"
XML Tutorials
; Storing properties in XML file This Example shows you how Store...;       Reading XML Data from... XML Tutorials      
Hibernate Data Filter using XML
In this section, you will learn to filter data using XML mapping file
xml problem - XML
xml problem  URGENT>>>>> how to retrieve data from hash tables without a JDBC connection?????? Thanks & Regards SAIBHARGAV146
Reg XML - XML
. It is a markup language much like HTML used to describe data. In XML, tags... Document Type Definition (DTD) , XML Schema to describe the data. Here...Reg XML  How can I become an XML programmer?What are the channels I
XML with JSP - XML
XML with JSP  Hai all,, I have a doubt..is it possible to submit jsp form data in to xml... i mean after filling the HTML form when i give submit it should save all the content in xml instead of DB. Then i want to fetch all
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
Retrieve data from xml using servlets
Retrieve data from xml using servlets  Hi plz send me the code for retrieving the data from xml File using Servlets.   Hi, Do you want... Thanks   Hi, Learn Get Data From the XML File. Thanks
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... the records line by line the data is not converted to String. And i want to know
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
storing date from html form to oracle 10g using servlet
storing date from html form to oracle 10g using servlet  i have following html form from where date,month and year is retrieved separately.. <... the data in oracle you need to follow the jdbc concept. here is the snippet
JSP and XML .data store nd retrieve
JSP and XML .data store nd retrieve  I have made a form in jsp having emp id, projectname and emp name.I want to store data related to employee in xml file.How can i store data entered by user in XML file and later retrieve data
how to store data in XML file - JSP-Servlet
how to store data in XML file  hi i have to store the data for example user id and password in a xml file the input userid and password..., To solve the problem visit to : http://www.roseindia.net/servlets/login-Xml

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.