How to remove Namespace and Nillable properties in

2019-03-02 07:42发布

问题:

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:

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:

  • A namespace declaration was written out. This is because the @XmlSchema annotation is present on the package-info class that maps the namespace qualification (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html).

    @javax.xml.bind.annotation.XmlSchema(
        namespace = "emp", 
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package emp;
    
  • The element corresponding to the centralCaseID property does not appear in the XML. This is the default behaviour regarding null properties (see: http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html).