I have to instrumentalize my code to generate tests. For that purpose, I use serialization to capture my objects state from the server while running some system tests. And I use the generated files for my IT-Tests using deserialization.
My colleague Fabrizio provided me with a neat utility that generates json files from all my serializable classes.
But I was confronted with difficulties with the complexity of some object graphs, so I use Xstream to generat xml.
Here's how to dump an object to xml:
XStream xstream = new XStream();
String xml = xstream.toXML(myObject);
String filePath = "output.xml";
try{
new File(filePath).createNewFile();
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "utf-8"))) {
writer.write(xml);
writer.flush();
}
} catch (IOException e){
e.printStackTrace();
}