We will show here a method to serialize an instance of a class to an XML file. We will use the java.beans package and contained there classes XMLEncoder and XMLDecoder.
First we need a class to serialize:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: |
public class SerializeMe {
public Integer data;
public void setData(Integer data) {
this.data = data;
}
public Integer getData() {
return data;
}
}
|
This simple class contains some data stored in the field data. As you see this class adheres to JavaBeans method specification, which is a necessary condition to use XMLEncoder on it (it does not however have to implement Serializable interface).
Next we write the main method that will serialize an instance of our class to XML and then deserialize it back:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: |
public class XMLSerialize {
public static void main(String[] args)
throws FileNotFoundException {
//Name of the file where we will store the object
String fileName = "Beanarchive.xml";
//XMLEncoder does all the job of encoding an object to an XML.
//It neads an output stream where it will save the XML so we
//supply it with a FileOutputStream, directing this way the
//output to the "Beanarchive.xml" file
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(fileName)));
//We create an instance of our class for serialization
SerializeMe sm = new SerializeMe();
sm.setData(10);
//This is it! Here we perform the serialization and the
//XML is written to the file
encoder.writeObject(sm);
encoder.close();
//XMLDecoder needs an input stream from which it can get the
//XML data to deserialize, so we supply it with a
//FileInputStream pointing at our "Beanarchive.xml" file.
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(fileName)));
//Here we decode the XML and create a new instance of SerializaMe
//filled with decoded data
SerializeMe decodedSm = (SerializeMe) decoder.readObject();
decoder.close();
//We check if the decoded data really is what we
//put into 'sm' before encoding.
System.out.println(decodedSm.getData());
}
}
|
When you run the program, you should get output ‘10′, proving that XML serialization and deserialization went well. If you look now inside the working directory, you will find the file “Beanarchive.xml” with our serialized object:
1: 2: 3: 4: 5: 6: 7: 8: |
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_0" class="java.beans.XMLDecoder">
<object class="SerializeMe">
<void property="data">
<int>10</int>
</void>
</object>
</java>
|
This short introduction shows just a little bit of what actually can be done with XML serialization. You can read much more about it for example here. We have also an entry on this blog about customizing the serialization process in general (using writeObject and readObject methods), which you can read here.
1 Comment until now
For many more powerful examples see my paper on Serialize Java Data Objects to XML
at this link:
http://docs.sheepdogsoftware.net/docs/tech_mgmt/webdev/objtoxml.html
Charlie
http://www.cdhconsult.com
Add your Comment!