If I would like to :
- annotate the list to be recognized as an implicit collection
- annotate an alias the desired class / fields
How-To
Here has sample code:
@Service public class XmlService { public String generateXmlString(Root xmlObject) { XStream xstream = new XStream(); // Process the annotations of the given types and configure the XStream. xstream.processAnnotations(new Class[] { Root.class, MyObjectList.class, MyObject.class }); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xstream.toXML(xmlObject); return xml; } // create an alias to the desired class @XStreamAlias("MyObject") @Data private static class MyObject { // ignore fields } @Data private static class MyObjectList { // whenever you have a collection which doesn't need to display it's root tag, // you can map it as an implicit collection @XStreamImplicit private List<MyObject> resultObjects; } @Data // create an alias to the desired class @XStreamAlias("TEST") private static class Root { // create an alias to the field @XStreamAlias("MyObjectList") private MyObjectList objectList = null; } }
The generated XML content looks like:
<?xml version="1.0" encoding="UTF-8"?> <TEST> <MyObjectList> <MyObject> </MyObject> <MyObject> </MyObject> </MyObjectList> </TEST>