3
votes

I'm working with an XSD schema file (that I cannot change) that defines element XXX as following:

<xsd:element name="XXX" type="Date" minOccurs="0"/>
This produces a java.util.Date in the generated class.

I would need to change how the field is marshalled to XML: if the attribute is null, I would like to produce an empty tag, like if the xsd was:

<xsd:element name="XXX" type="Date" minOccurs="0" nillable=true/>
This produces a JaxbElement in the generated class.

Is it possible to do something like this via Jaxb bindings?

Regards
Giulio

2

2 Answers

3
votes

Yes you can.. but isn't a good practice.

<bindings node="//xs:element[@name='XXX']">
    <property name="xxx">
        <baseType> 
            <javaType name= "javax.xml.bind.JAXBElement&lt;java.util.Date&gt;"/>
        </baseType>
    </property>
</bindings>

you should add also below attributes within <javaType> See here Documentation

  • parseMethod is the name of the parse method to be called during unmarshalling.
  • printMethod is the name of the print method to be called during marshalling.
3
votes

Suggestions:

  • Pre-process your schema with an XSLT to add nillable where you need it.
  • Use the jaxb2-simplify-plugin and customize your element with simplify:as-reference-property. I have actually never tried that but maybe it'll work.
  • Write an XJC plugin.

ps. I'm the author of the mentioned jaxb2-simplify-plugin.