1

I'm creating a JAXBElement<BigInteger> element with an ObjectFactory.

The object is created with a null value, as the schema for this field indicates that it has a nillable attribute.

During the marshalling to xml, the output file shows:

<TaxCode xsi:nil="true"/>

How can I have it write <taxCode/> only?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Assaf Adato
  • 237
  • 4
  • 14

1 Answers1

2

You can't, because that would be wrong. <taxCode/> and <TaxCode xsi:nil="true"/> are not the same thing.

<taxCode/> means a <taxcode> element with blank but not-null content (i.e. an empty string), whereas <TaxCode xsi:nil="true"/> is explicitly saying <taxCode> is null.

Assuming that your ObjectFactory was generated by XJC from an XML Schema, then if JAXB were to produce <taxCode/> then it would be in violation of the schema.

If changing the schema is an option, then remove nillable from the element declaration, and regenerate the code. JAXB should then omit the <taxCode/> element altogether.

skaffman
  • 398,947
  • 96
  • 818
  • 769