XMLSerializer Usage
This tools let you serialize an Java object in XML.
Requierments
- java >= 1.4
- xerces parser
Installation / Download
Download the last version of XMLSerializer.jar from sourceforge.net.
If you don't have xerces parser, download also JavaXML.jar.
Usage
Your saving/loading code must import fr.inpg.XMLSerializer.XMLSerializer
.
Example of saving :
import fr.inpg.XMLSerializer.XMLSerializer ;
public void saveXML(Object object, String theFile) {
XMLSerializer xmlSerializer = null;
try {
xmlSerializer = new XMLSerializer("xmlModel"); // A new XMLSerializer using the file `xmlModel' as model file
}
catch(Exception e) {
e.printStackTrace(); // probably the file has not been found
}
try{
xmlSerializer.xmlSerialize(object, theFile) ; // saving the xml dump of `object' to the file `theFile'
}
catch(Exception e) {
e.printStackTrace();
}
}
Example of loading :
import fr.inpg.XMLSerializer.XMLSerializer ;
public Object loadXML(String theFile) {
XMLSerializer xmlSerializer = null;
try {
xmlSerializer = new XMLSerializer("xmlModel"); // A new XMLSerializer using the file `xmlModel' as model file
}
catch(Exception e) {
e.printStackTrace(); // probably the file has not been found
}
try{
return xmlSerializer.xmlDeserialize(theFile) ; // loading the xml dump from the file `theFile'
}
catch(Exception e) {
e.printStackTrace();
return null ;
}
}
More details
Default behaviour
By default XMLSerializer serializes all fields (public, protected, private, final, satic...) and all parents of the object. Be careful, never serialize big components like Swing components or your XML dump will certainly be larger than 3Megs just for one object !
Customization
You can custum the way an object is saved by a model file. This file contains the directive to save an object. The syntax is the following :
<?xml version="1.0" encoding="UTF-8"?>
<model>
<class name="SpecificFieldsSaved" saveParent="false" which="followings" >
<field>assignNum</field>
<field>disabled</field>
<field>id</field>
<field>name</field>
<field>selected</field>
</class>
<class name="SpecefiecFieldsSavedAndParent" saveParent="yes">
<field>disabled</field>
</class>
<class name="NoFieldSaveButParentSaved" saveParent="true" />
<class name="NothingSaved" />
<class name="AllFieldsSaved" which="all" />
<class name="NothingSavedToo" which="none" />
<class name="AllFieldsSavedButTheSpecified" which="all-but" >
<field>badField</field>
</class>
</model>
If a attribute is not present for a class
tag, the default behaviour is saveParent="true"
et which="followings"
.
The class
tag requiers the name
attribute which specify the name of the class you want have a special save behaviour. It should always be full determined like fr.inpg.XMLSerializer.XMLSerializer
and not XMLSerializer
only. which
and saveParent
are optional attributes.
field
tags can be inserted between class
tags. It represents a field in the class which has to be saved or not depending of the which
attribute. No attribute for field
tag is needed.
Exemple of customization
We have the following Java class to save :
package myPackage ;
import javax.swing.* ;
public class MyClass extends MyFatherClass {
private String name ;
private static int ID = 0 ;
protected static int id ;
public int type ;
public JDialog configureDialog ;
private Vector bigVector ;
private static final Hashtable hashtable = new Hashtable() ;
// and a lot of methods...
}
In such an object, we want to save all fields but configureDialog because it is a Swing component that is huge.
Even if hashtable is static and final, we must save it because it contains elements that could not be re-build during the load process.
It is the same note for ID property which is static but must be saved not to begining with 0 the next time this object is loaded.
It is not a problem if this class does not have a no-args constructor, it will be create "on-the-fly" during the load process. But if this class has a no-args constructor, it will be used and no constrctor will be added.
The xml model file will be :
<?xml version="1.0" encoding="UTF-8"?>
<model>
...
<class name="myPackage.MyClass" saveParent="true" which="all-but" >
<field>configureDialog</field>
</class>
...
</model>
Limitations
It is impossible to serialize some java elements like :
- java.util.HashMap-like,
- java.util.HashSet-like,
- java.util.Random,
- java.reflect.Class,
- and certainly more...
It is uncertain to serialize Swing components.
Known bugs
See limitations.
sylvain tedoldi