Storing Data (Retrieved from a XML Document) to a File

In this section, you will learn to store data
(retrieved from the XML document) to a specified file (with
extension '.txt',
'.doc', '.xls', '.shtml' etc.) in different formats (text,
xml, html etc.).
Description of program:
This is a very simple program that helps you in storing
the data to a specified file in different format. After running this program
asks you a xml file name at the console. If the given file exists it
parses and creates a Document object . To parse it you need the DocumentBuilderFactory
and DocumentBuilder object. We create a Transformer and use the setOutputProperty()
method that is an abstract method to invoke the Transformer object and
sets an output property that will generate a output in desired format. In this
method you set the "text" value for generating the text
retrieved from the
xml document.
An object of Document is passed in the DOMSource() constructor
. Result object is created to show the generated file as output. Here we
give a file name with extension, it creates a new file according to given file
name and extension. The transform() method takes the Source and Result
objects and it processes the source object to the output result. When the file is
created it displays a message "File creation successfully!"
Here the results are displayed in different files like: vk.txt,
vk.doc, vk.xls, vk.shtml
etc. from the XML document.
Here is the XML File: Employee-Detail.xml
<?xml version = "1.0" ?>
<Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Sushil </Emp_Name>
<Emp_E-mail>Sushil@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail> |
Here is the Java File: StoreData.java
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class StoreData{
static public void main(String[] arg) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
// Create transformer
Transformer tFormer = TransformerFactory.newInstance().newTransformer();
// Output Types (text/xml/html)
tFormer.setOutputProperty(OutputKeys.METHOD, "text");
// Write the document to a file
Source source = new DOMSource(doc);
// Create File to view your xml data as
vk.txt/vk.doc/vk.xls/vk.shtml/vk.html)
Result result = new StreamResult(new File("vk.txt"));
tFormer.transform(source, result);
System.out.println("File creation successfully!");
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.err.println(e);
System.exit(0);
}
}
}
|
Download this example.
Output of program:
C:\vinod\xml>javac StoreData.java
C:\vinod\xml>java StoreData
Enter XML file name: Employee-Detail.xml
File creation successfully!
C:\vinod\xml> |

|
Current Comments
3 comments so far (post your own) View All Comments Latest 10 Comments:Xcellent programming was given it is very useful ....... thank u ...
Posted by srihari on Thursday, 01.3.08 @ 14:08pm | #44428
can u please design a program for me.
the question is as follows:
The idea of the project is to make a Personnel Information System. Create text file which contains employees' names, id, office number, phone number, email and salary. They have to read the content of the file and save it in an array. Also, they have to make class called Employee and a Main application class for it. The features provided by the system are
• Welcome Screen and Login Screen.
• Allow only the administrator to:
o Search for an employee by name.
o Search for an employee by id.
o Modify employees' information.
o Reset user password.
o Add new employee to text file with a user account.
o Delete record from the text file.
o Display all records in the text file.
• Allow the normal employee to login and view his information.
jus use simple arrays and file I/O..
ur response wil b appreiated.
Posted by Umair on Tuesday, 08.7.07 @ 17:47pm | #22872
how to display data retrived through jsp in xml format
Posted by sandesh on Thursday, 07.19.07 @ 15:07pm | #21565