Programming - Java XML Serialization
XML Serialization is an excellent way to store simple data to
a file. It also has the benefit of being human readable. It
can be used for any purpose and is especially useful for
configuartion files. we have previously discussed
serialization in C#, but it is also useful in Java.
We are looking at the following
OpenSource
Java library, whcih is by far the eastiest Object
Serialization class I have seen in Java.
http://xstream.codehaus.org/
Here is an example:
public class Animal{
and public StringHairColour;
and public StringEyeColour;
and public Animalpartner;
and // Constructors and methods here
}
XStream serialiser= new XStream();
serialiser.alias("animal", Animal
.class);
Animal
l ion = new Animal();
lion.EyeColour= "Blue";
lion.HairColour = "Gold";
and
Animal
l ion = new Animal();
lion.partner=and new Animal();
lion.partner.EyeColour= "Blue";
lion.partner.HairColour = "Gold";
String xmlised = serialiser.toXML(lion);
// and to retrieve an object back :
Animal
l ion_copy = (Animal)xstream.fromXML(xmlised );and
Its all pretty simple. You can then dump this XML string to a
file or store it in a database. Its totally up to you.
and
and
and
and