XmlRootElement auto wrapper for list

I have a simple class that I need to sort. The class is declared as:

@XmlRootElement public class XMLUser... 

Here is what I get:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xmlUser> <login>myLogin</login> <password>myPass</password> <role name="role1"/> <role name="role2"/> <role name="role3"/> </xmlUser> 

Now I want to have several users in one file, but without the need to create a wrapper class myself, kind of like using @XmlElementWrapper, but for a class instead of a field. I do not know if this is possible.

So that I can sort the list (or some object provided by jaxb) and I could get this XML code (the <users> generated automatically):

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <users> <xmlUser> <login>login1</login> <password>pass2</password> <role name="role1"/> <role name="role2"/> </xmlUser> <xmlUser> <login>login2</login> <password>pass2</password> <role name="role1"/> <role name="role3"/> </xmlUser> </users> 

Any help is appreciated.

+4
source share
1 answer

This is not possible without creating a new class.
one way

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Users { @XmlElement(name = "xmlUsers") List<XmlUser> users = new ArrayList<XmlUser>(); } 

output

 <users> <xmlUsers> //... </xmlUsers> <xmlUsers> //... </xmlUsers> </users> 
+5
source

Source: https://habr.com/ru/post/1432179/


All Articles