Im just wondering how i use XMLEncoder
to serialize ArrayList<foo>
where foo is my own made class.
Do i have to do anything in particular, ie define my own xml structure first and then call toString on each value in my list and write it out?
Can anyone point me to a good tutorial? http://java.sun.com/products/jfc/tsc/articles/persistence4/ Thats what i have been looking at but it doesnt seem to mention what to do with non library classes.
Thanks
Serialization of Java Objects to XML can be done using XMLEncoder, XMLDecoder. Java Object Serialization feature was introduced in JDK 1.1. Serialization transforms a Java object or graph of Java object into an array of bytes which can be stored in a file or transmitted over a network.
Java Serialization Methods That's why there are four methods that we can provide in the class to change the serialization behavior.
The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.
To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.
If you are looking for XML Serialization I would suggest you to go for XStream
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>
There is nothing special about serializing an ArrayList with XMLEncoder.
Here is an example:
There is a bean class TestBean:
public class TestBean {
private String name;
private int age;
public TestBean() {
this.name = "";
this.age = 0;
}
public TestBean(String name, int age) {
this.name = name;
this.age = age;
}
// Getter and setter ...
@Override
public String toString() {
return String.format("[TestBean: name='%s', age=%d]", name, age);
}
}
And a class Main which serialize an ArrayList<TestBean>
and read it back again:
public class Main {
private static final String FILENAME = "testbeanlist.xml";
public static void main(String[] args) {
try {
// Create a list of TestBean objects ...
final List<TestBean> list = new ArrayList<TestBean>();
list.add(new TestBean("Henry", 42));
list.add(new TestBean("Tom", 11));
System.out.println("Writing list to file " + FILENAME + ": " + list);
// ... and serialize it via XMLEncoder to file testbeanlist.xml
final XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(FILENAME)));
encoder.writeObject(list);
encoder.close();
// Use XMLDecoder to read the same XML file in.
final XMLDecoder decoder = new XMLDecoder(new FileInputStream(FILENAME));
final List<TestBean> listFromFile = (List<TestBean>) decoder.readObject();
decoder.close();
System.out.println("Reading list: " + listFromFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
And then the output is:
Writing list to file testbeanlist.xml: [[TestBean: name='Henry', age=42], [TestBean: name='Tom', age=11]]
Reading list: [[TestBean: name='Henry', age=42], [TestBean: name='Tom', age=11]]
The toString()
method in TestBean
is only for pretty printing. It doesn't influence the XML serialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With