I have an XML Schema that says:
<xs:element name="employerOrganization" nillable="true" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
<xs:attribute name="classCode" type="EntityClassOrganization" use="required"/>
<xs:attribute name="determinerCode" type="EntityDeterminerSpecific" use="required"/>
</xs:complexType>
</xs:element>
That means I must be able to create an instance that looks like this:
<employerOrganization classCode="ORG" determinerCode="INSTANCE" xsi:nil="true"/>
According to the XML Schema spec I can (http://www.w3.org/TR/xmlschema-0/#Nils). According to Microsoft .Net I cannot (http://msdn.microsoft.com/en-us/library/ybce7f69(v=vs.100).aspx) and as far as others tell me Jaxb cannot either.
Are both .Net and Jaxb uncompliant? Can I override somehow to get the desired output?
In JAXB you can leverage a
JAXBElement
for this. TheJAXBElement
can hold a value which has fields/properties mapped to XML attributes and a flag that tracks whether the element was nil.Java Model
Foo
Instead of having a field/property of type
Bar
you specifyJAXBElement<Bar>
.Bar
Bar has fields/properties mapped to XML attributes.
ObjectFactory
Demo Code
Demo
input.xml/Output