I have following XSD file:-
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="emp" targetNamespace="emp"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Case_Detail" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Case_Detail">
<xs:complexType>
<xs:sequence>
<xs:element ref="Central_Case_ID"/>
<xs:element ref="Agency_Case_ID"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Central_Case_ID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Agency_Case_ID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
I have generated JAXB classes and trying to marshal into XML file. In generated XML file, I can for each elements in XML file, attributes are coming if it is null like :- For e.g. :-
<Central_Case_ID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
My requirement is to have elements without these attributes at all even if some element has null values in the enitre XML file. For this I have tried alot, I made lot of changes in XSD like updated elementFormDefault="unqualified" and arguementFormDefault to unqalified but it didn't work. Then I made changes in @annotations in JAXB classes and also tried to set some properties in marshalling object like No_Namespace_schema_location but nothing worked.
Kindly suggest.
Java Model
I generated the default Java model from your XML Schema using XJC.
Demo Code
Demo
The demo code below populates the object model (setting the
centralCaseID
property tonull
) and marshals it to XML.Output
Below is the output from running the demo code.
Items to note:
A namespace declaration was written out. This is because the
@XmlSchema
annotation is present on thepackage-info
class that maps the namespace qualification (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html).The element corresponding to the
centralCaseID
property does not appear in the XML. This is the default behaviour regardingnull
properties (see: http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html).