• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Persisting Object State in Xml Format

September 25, 2007 //  by Krishna Srinivasan//  Leave a Comment

Persistence is the process of saving the state of an object permanently to a storage like file or database, and the state of the object can be restored at a later time. In Java terms, Persistence is nothing but Serialization. For example, the following code is used to save the state of an object in a file.

also read:

  • XML Tutorials
  • How to Parse XML file using DOM Parser?
  • How to Parse XML file using SAX Parser?

Assuming that there is some class called Flower implementing java.io.Serializable.

public class Flower implements Serializable {

	private String name;
	private String colour;

	public Flower(){
	}

	public Flower(String name, String colour){
		this.name = name;
		this.colour = colour;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString(){
		return "The " + name + " is " + colour + " in colour.";
	}
}

The following code shows how to persist an object of the class Flower.

    ObjectOutputStream output = new ObjectOutputStream(
        new FileOutputStream("flower"));
    output.writeObject(new Flower("Orchid","Purple"));
    output.close();

If we want to re-construct the original object from the file "flower", then the following piece of code will help.

    ObjectInputStream input = new ObjectInputStream(
        new FileInputStream("flower"));
    Flower orchid = (Flower)input.readObject();
    System.out.println(orchid);

If we try to open the file "flower", nothing can be understood from that, since the format of file is binary.

The output of reconstructing the object from file is,

The Orchid is Purple in colour.

We have just seen a simple example of persisting an object. Let us now get into our topic of interest , i.e Persisting Object State in Xml Format. The following program shows how to save and restore the state of an object in an appropriate way by using Xml format.

    FileOutputStream fileOutputStream = new FileOutputStream("flower.xml");
    XMLEncoder encoder = new XMLEncoder(fileOutputStream);
    Flower orchid = new Flower("Rose","Red");
    encoder.writeObject(orchid);
    encoder.close();

The above code makes use of java.beans.XMLEncoder to save the object state in Xml Format. Given below is the code to read the xml file content for re-constructing the object.

    XMLDecoder decoder = new XMLDecoder(new FileInputStream("flower.xml"));
    Flower flower = (Flower)decoder.readObject();
    System.out.println(flower);

The output of reconstructing the object from the flower.xml file is,

The Rose is Red in colour.

Category: XMLTag: XML Format

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Randomly accessing the file contents
Next Post: Parsing XML Documents using SAX »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact