'How to write list of elements to a XML without surrounding them into object?

I have a following model:

@Data
@XStreamAlias("id")
public class Group {
    private String id;
    private List<Member> memberList = new ArrayList<>();
}

Which after I parse a list of Groups to XML, it will print the following:

<group id="..">
  <memberList>
      <member>
       ...
      </member>
      <member>
       ...
      </member>
      <member>
       ...
      </member>
  </memberList>
</group>

However, I would like to to save the list without the surrounding object memberList:

<group id="..">
  <member>
    ...
  </member>
  <member>
    ...
  </member>
  <member>
    ...
  </member>
</group>

How can I achive that? I want to be able to handle lists of many different objects.



Solution 1:[1]

Never used X-Stream before, but based on its documentation http://x-stream.github.io/annotations-tutorial.html , you can make use of @XStreamImplicit annotation

@Data
@XStreamAlias("id")
public class Group {
    private String id;

    @XStreamImplicit(itemFieldName="member")
    private List<Member> memberList = new ArrayList<>();
}

Solution 2:[2]

If you are using org.simpleframework.xml and its @Root and @ElementList annotations, you can simply put @ElementList(inline=true) to get rid of additional wrapping elements in the list. It determines whether the element is inlined to the parent XML element.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian Shum
Solution 2 procrastinator