Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB: Empty string does not produce empty element

Tags:

jaxb

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(""); ):

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?

like image 833
Alexandros Avatar asked Jul 06 '10 13:07

Alexandros


1 Answers

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:

  • http://www.eclipse.org/eclipselink/downloads/nightly.php
like image 103
bdoughan Avatar answered Oct 15 '22 21:10

bdoughan