Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

User:Rick.barkhouse.oracle.com/Test1

XmlElementWrapper and empty and null Collections

In EclipseLink 2.4, MOXy's handling of empty and null Collections annotated with XmlElementWrapper has changed, to mirror the functionality of the JAXB reference implementation.

  • Empty Collections annotated with XmlElementWrapper will now be marshalled as an empty wrapper element.
  • Null Collections annotated with XmlElementWrapper(nillable = true) will now be marshalled as an wrapper element with xsi:nil="true".

The examples below will use the following domain class:

@XmlRootElement
@XmlAccessorType(AccessType.FIELD)
public class PurchaseOrder {
 
   String id;
 
   String customerName;
 
   @XmlElementWrapper(name = "line-items", nillable = true)
   List<LineItem> item = null;
 
}


Null Collections

PurchaseOrder po = new PurchaseOrder();
po.customerName = "Sarah Connor";
po.id = "FDKJHG387SKUH29887";
 
marshaller.marshal(po, System.out);

The null collection will be marshalled as an empty wrapper element with xsi:nil="true":

<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrder>
   <id>FDKJHG387SKUH29887</id>
   <customerName>Rick Barkhosue</customerName>
   <line-items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</purchaseOrder>

In versions of EclipseLink prior to 2.4, the line-items element would be absent.


Empty Collections

PurchaseOrder po = new PurchaseOrder();
po.customerName = "Sarah Connor";
po.id = "FDKJHG387SKUH29887";
po.item = new ArrayList<LineItem>();
 
marshaller.marshal(po, System.out);

The empty collection will be marshalled as an empty wrapper element:

<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrder>
   <id>FDKJHG387SKUH29887</id>
   <customerName>Rick Barkhosue</customerName>
   <line-items/>
</purchaseOrder>

In versions of EclipseLink prior to 2.4, the line-items element would be absent.


Configuration

If you would like to keep the old EclipseLink behaviour, you can use an XmlNullPolicy annotation:

Using Annotations:

@XmlElementWrapper(name="line-items", nillable=true)
@XmlNullPolicy(shouldMarshalEmptyCollections=false)
List<LineItem> item = null;

Using EclipseLink XML Bindings:

<xml-element java-attribute="item"/>
   <xml-element-wrapper name="line-items" nillable="true" />
   <xml-null-policy should-marshal-empty-collections="false" />
</xml-element>

By specifying (shouldMarshalEmptyCollections = false), neither null nor empty Collections will appear in marshalled XML.

Back to the top