tags:

views:

81

answers:

2

Hello,

I'm using JAXB 2.1.10 from Sun's JDK 1.6.0_18:

D:\apps\jdk160_18\bin>xjc.exe -version xjc version "JAXB 2.1.10 in JDK 6" JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build JAXB 2.1.10 in JDK 6)

I need to have JAXB's marshaller produce an empty element (e.g. <someStringField></someStringField> or <someStringField/>) when the JAXB object has the value of the empty string (""). However, rather than doing that, JAXB omits the element altogether from its output (as if it where an optional element).

My searches in the Internet indicated that JAXB should normally create this tag, as long as you set the field to the non-null empty string (i.e. myJAXBObject.setSomeStringField(""); ):

http://stackoverflow.com/questions/594537/how-to-instantiate-an-empty-element-with-jaxb https://jaxb.dev.java.net/tutorial/section_2_2_12_8-No-Value.html#No%20Value

In my XSD, I've tried to indicate (in every way I know) that the element's presence is mandatory even if it is empty:

      <xs:element name="outerElement">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="someStringField" type="xs:string" nillable="false" minOccurs="1" />
            <xs:element name="someOtherStringField" type="xs:string" />

The generated code looks like this (it's the same for both elements):

    @XmlElement(name = "someStringField", required = true)
    protected String someStringField;
    @XmlElement(name = "someOtherStringField", required = true)
    protected String someOtherStringField;

However, when I marshal the following object...

outerElement.setSomeStringField("");
outerElement.setSomeOtherStringField("Value was set");

I get:

<outerElement>
           <someOtherStringField>Value was set</someOtherStringField>
</outerElement>

When I was expecting:

<outerElement>
           <someStringField></someStringField>
           <someOtherStringField>Value was set</someOtherStringField>
</outerElement>

Or:

<outerElement>
           <someStringField/>
           <someOtherStringField>Value was set</someOtherStringField>
</outerElement>

Can anyone spot what I'm doing wrong?

A: 

Apologies. I THOUGHT I was using JAXB from Sun's JDK, but I wasn't. There was a left-over jaxb.properties in my classes/ folder that was created by JDeveloper when I generated the bindings. I was actually using JAXB2.0 from EclipseLink/MOXy.

Removing the properties file allowed JAXB2.1 to be used and fixed my issue.

Alexandros
This issue (https://bugs.eclipse.org/319028) has now been fixed in EclipseLink MOXy.
Blaise Doughan
A: 

Thank you for bringing this issue (https://bugs.eclipse.org/319028) to our attention. The bug has been fixed and will be included in the EclipseLink 2.1.1 maintenance release. If you want access to this fix earlier you can pick up the nightly download starting July 8th from:

Blaise Doughan