Use the PrettyPrintWriter
to get the pretty format along with a StringWriter
as follows:
public static String java2xml(Object obj, Class<?> T) {
XStream xstream = new XStream(new StaxDriver());
xstream.processAnnotations(T);
StringWriter stringWriter = new StringWriter();
xstream.marshal(obj, new PrettyPrintWriter(stringWriter));
return stringWriter.toString();
}
As for the <outer-class reference="../.."/>
issue, if you are not referencing any of Foo
class members from Bar
you can get rid of the <outer-class>
tag by defining the Bar
class as static