11

I have a java property annotated with @XmlElement(required=false, nillable=true). When the object is marshalled to xml, it is always outputted with the xsi:nil="true" attribute.

Is there a jaxbcontext/marshaller option to direct the marshaller not to write the element, rather than write it with xsi:nil?

I've looked for answers to this and also had a look at the code, afaics, it will always write xsi:nil if nillable = true. Am I missing something?

dur
  • 15,689
  • 25
  • 79
  • 125
Keith
  • 113
  • 1
  • 1
  • 5

1 Answers1

6

If the property is annotated with @XmlElement(required=false, nillable=true) and the value is null it will be written out with xsi:nil="true".

If you annotate it with just @XmlElement you will get the behaviour you are looking for.

import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;

Example

Given the following class:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement(nillable=true, required=true)
    private String elementNillableRequired;

    @XmlElement(nillable=true)
    private String elementNillbable;

    @XmlElement(required=true)
    private String elementRequired;

    @XmlElement
    private String element;

}

And this demo code:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        marshaller.marshal(root, System.out);
    }

}

The result will be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <elementNillableRequired xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <elementNillbable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</root>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks, that's what I was seeing in the code as well. I didn't want to change the annotations since it was generated code, and also because it is an accurate reflection of the xml schema (which defined the elements as minoccurs='0' and nillable='true'). – Keith May 09 '11 at 16:42
  • 3
    FWIW, it appears that `@XmlElement(required=false)` also produces the desired behavior. – Matt Ball May 12 '11 at 19:58
  • Hrmmm ... I know this is old but, the elementRequired being null should actually throw an error (right?) ... because its null yet required. In my case, we are using cxf / SOAP. – Jack Oct 25 '12 at 16:06
  • I try to extend your example where elementNillableRequired element has atribute and then it doesnt work. It create invalid element. I just set atribute and let value be null: (nillable is missing) – hudi Feb 16 '15 at 09:39
  • @hudi - I will take a look at the corresponding question you posted: http://stackoverflow.com/questions/28537873/jaxb-create-invalid-nillable-element-when-generate-xml-against-xsd – bdoughan Feb 16 '15 at 10:19
  • @BlaiseDoughan did you already check my question ? I would be very grateful for your help – hudi Feb 24 '15 at 09:17