1

I have following attribute in my XSD:

<xs:attribute name="fromFraction" type="xs:float" use="optional" default="0.0"/>

I generate class from this XSD and attribute is created as float, and then I can't marshall XML without this attribute from this class (as far as I understand, in that case it will always have some value which will be marshalled).

How can I change the XSD to create a Float type during binding, so I can have null in this field (so it could be omitted during marshalling)?

Or maybe there is a better way to remove this attribute from resulting XML when it isn't present?

Line
  • 1,529
  • 3
  • 18
  • 42
  • Have you seen [this](https://stackoverflow.com/questions/13035975/jaxb-compiler-is-binding-xsboolean-to-java-boolean-wrapper-class-instead-of-bo?rq=1)? Seems to have the opposite problem. – daniu Aug 24 '17 at 09:23
  • 1
    @daniu - sure, the difference is that there he has an element, not attribute. I tried but I can't set minOccurs on attribute, however parent element is requied. but it doesn't have to have this particular attribute. so I think that isn't completely the case. – Line Aug 24 '17 at 09:32
  • @dsp_user - In JAXB classes, when the field type is primitive-wrapper (like Float instead of float) and it is set to null, attribute is not present in marshalled XML at all. That's the result I expect. – Line Aug 24 '17 at 12:13
  • What do you mean by 'I can't marshal XML whithout this attribute..'? If you explicitly don't set the attribute's value, it will be set by Java to the type's default value (0.0 in your case and will be marshalled as such). Perhaps you can show us some code. – dsp_user Aug 24 '17 at 12:17
  • I wa going to suggest that you can use an XmlAdapter but it can't work with either float or Float (it expects a String). So perhaps you can do this if you can use a String instead of a Float (of course, the string will still store a Float ) – dsp_user Aug 24 '17 at 12:22
  • I can write some code if you think that'll help you. – dsp_user Aug 24 '17 at 12:26
  • @dsp_user - you want me to use String instead of float in source XSD? – Line Aug 24 '17 at 12:33
  • Yes, that's the idea but I've just tried it and it still doesn't remove the attribute when it's null (even when using an XmlAdapter). You may want to take a look here https://stackoverflow.com/questions/25643097/can-a-part-of-xml-be-marshalled-using-jaxb-or-jaxb-stax – dsp_user Aug 24 '17 at 12:36
  • Unfortunately, an XmlAdapter will not help you here because it can only modify and format existing attributes, elements etc but cannot leave them out of the resulting xml. – dsp_user Aug 24 '17 at 12:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152758/discussion-between-line-and-dsp-user). – Line Aug 24 '17 at 13:00

2 Answers2

1

According to this: http://reast.net/2009/08/jaxb-generating-primitive-type-getters/ and its reference: http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/jaxb/vendorCustomizations.html#serializable

you should add this to your XSD:

<xs:annotation>
   <xs:appinfo>
      <jaxb:globalBindings>
          <xjc:serializable uid="12343"/>
      </jaxb:globalBindings>
   </xs:appinfo>
</xs:annotation>
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • The first link won't load for me. Can you explain how serialization is connected with my problem with types? – Line Aug 24 '17 at 09:52
  • The first link explains why to remove the attribute from `jaxb:globalBindings`. – Timothy Truckle Aug 24 '17 at 09:55
  • are you sure that you found solution to my problem there? because from what I see, this problem is basically about getters/setters, while the parameter type is already primitive-wrapper, and that is what I can't achieve. – Line Aug 24 '17 at 10:23
1

I had following line in binding.xjb file which I used to generated classes:

<jxb:globalBindings optionalProperty="primitive"/>

When I removed it, generated classes contain proper primitive-wrappers, just as I wanted.

Line
  • 1,529
  • 3
  • 18
  • 42