Q:

jaxb xs:element ignore empty list

i have a schema file like
 <xs:complexType name="Autorisation">
    <xs:sequence>
        <xs:element name="roles" type="Roles"/>
    </xs:sequence>
 </xs:complexType>

<xs:complexType name="Roles">
    <xs:sequence>
        <xs:element name="role" type="Role" maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Role">
  <xs:sequence>
    <xs:element name="type" type="xs:int"/>
    <xs:element name="dateDebut" type="xs:dateTime"/>
    <xs:element name="dateFin" type="xs:dateTime"/>
  </xs:sequence>
</xs:complexType>

it generate something like that :
@XmlElementWrapper
@XmlElement(name = "role")
protected List<Role> roles;

When i create an xml file with JAXB.marshal(input, Output); with an empty roles list, the xml file generated contains <role/>
How can i avoid that ?
Thanks !! :)

A:

@XmlElementWrapper
@XmlElement(name = "role")
protected List<Role> roles;

Then one of the following will happen:

If roles contains items you will get:
<roles>
    <role>...</role>
    <role>...</role>
</roles>

If roles is an empty list you will get:
<roles/>

If roles is null then the roles element will not appear.

For More Information
I have written more about this on my blog:

http://blog.bdoughan.com/2012/12/jaxb-representing-null-and-empty.html

UPDATE

I also use "dozer" to convert Object into another object, so empty
  list are never null. Is there no way to tell JAXB ignore empty list ?

You could add some marshal event call back methods on your domain class to adjust the List property before and after marshalling (see:  https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Marshaller.html#marshalEventCallback).
void beforeMarshal(Marshaller) {
  if(roles != null && roles.isEmpty()) {
      roles = null;
  }
}

void afterMmarshal(Marshaller) {
    if(null == roles) {
        roles = new ArrayList<Role>();
    }
}

