Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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>

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.