Serializing Java Objects to XML

HI !

in my last post i talked about java -Costume- Serialization, so for today i will continue on that topic but this time i will write objects to a more human readable format “XML”.

today’s post is more of an introduction to a class (two classes actually ) XMLEncoder and XMLDecoder .

XMLEncoder is an complementary alternative to the ObjectOutputStream Class . instead of series of bytes you get a textual format .

XMLEncoder Write Objects with the Java Beans Convention in mind, it’s not necessary to implement the Serializable interface, but your Object must have these two properties :

1- public empty constructor

2- getters and setters for every protected/private property

as per how the XMLEncoder works , it uses reflexion to know the state of each field and convert them to XML format, let’s look at an example to make it clearer :

let’s Say we have a Settings Class that contains information about a certain user :


// simple class written as a Java bean
public class Settings {
private String settings1;
private Integer settings2;
public Settings(){}
public Settings(String settings1, Integer settings2) {
this.settings1 = settings1;
this.settings2 = settings2;
}
public String getSettings1() {
return settings1;
}
public void setSettings1(String settings1) {
this.settings1 = settings1;
}
public Integer getSettings2() {
return settings2;
}
public void setSettings2(Integer settings2) {
this.settings2 = settings2;
}
@Override
public String toString() {
return "Settings{" +
"settings1='" + settings1 + '\'' +
", settings2=" + settings2 +
'}';
}
}

view raw

Settings.java

hosted with ❤ by GitHub

now we Create A Serializer class that will handle the serialization and deserialization process :


import java.beans.ExceptionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author Mehdi Maick
* Created by mac on 17/11/2016.
*/
public class Serializer {
// Serialize the given Object
public static void serializeToXml(Settings settings) throws Exception {
FileOutputStream f = new FileOutputStream("settings.xml");
XMLEncoder encoder = new XMLEncoder(f);
encoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception e) {
System.out.println("an Exception Occured " + e.getMessage());
}
});
encoder.writeObject(settings);
encoder.close();
f.close();
}
// Deserialize an Object
public static Settings desrializeFromXml() throws Exception {
FileInputStream fis = new FileInputStream("settings.xml");
XMLDecoder decoder = new XMLDecoder(fis);
Settings decodedSettings = (Settings) decoder.readObject();
decoder.close();
fis.close();
return decodedSettings;
}
//main to test
public static void main(String[] args) throws Exception {
Settings s = new Settings("user settings", 98);
serializeToXml(s);
Settings same = desrializeFromXml();
System.out.println(same);
}
}

view raw

Serializer.java

hosted with ❤ by GitHub

this will create a settings.xml file this is the content :


<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_60" class="java.beans.XMLDecoder">
<object class="lib.Settings">
<void property="settings1">
<string>user settings</string>
</void>
<void property="settings2">
<int>98</int>
</void>
</object>
</java>

view raw

settings.xml

hosted with ❤ by GitHub

magic isn’t it .

Well there you have it Writing Objects to Xml for more advanced Usage please check out this tutorial .  Happy Coding 😀 …

 

Leave a comment