Serialize/Deserialize object to/from XML

Započeo maxogm, 26.12.2010, 23:07

prethodna tema - sledeća tema

maxogm

Serialize object to XML:

public String encode(Object input){
ByteArrayOutputStream xml = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(xml);
encoder.writeObject(input);
encoder.close();
return xml.toString();
}


Deserialize object from XML:

public Object decode(String input){
byte[] bytes = input.getBytes();
ByteArrayInputStream ai = new ByteArrayInputStream(bytes);
XMLDecoder decoder = new XMLDecoder(ai);
return decoder.readObject();
}