2

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.

1 Answer 1

1

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 to null) and marshals it to XML.

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("emp");

        ObjectFactory objectFactory = new ObjectFactory();
        Root root = objectFactory.createRoot();

        CaseDetail caseDetail = objectFactory.createCaseDetail();
        caseDetail.setCentralCaseID(null);
        root.getCaseDetail().add(caseDetail);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

Output

Below is the output from running the demo code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns="emp">
    <Case_Detail/>
</root>

Items to note:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.