Converting nil = "true" to null when working with non-marshal

I get XML from a server whose schema defines almost every element as:

<xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0"/> <xs:element name="myIntElementName" type="xs:int" nillable="true" minOccurs="0"/> 

I am trying to find a clean way to convert every element that I get that is marked as xsi:nil="true" to null when it is not sorted into a JAXB object. So something like this:

 <myIntElementName xsi:nil="true" /> 

Should result in my JAXB object having the myIntElementName property with a value of null, and not with a JAXBElement object with the nil property set to true (or something in that direction). I have no control over a system that sends me XML that uses the nillable attribute, so I need to convert this at my end when I receive it.

+3
java xml jaxb jaxb2 xml-nil
source share
1 answer

@XmlElement (nillable = true)

You just need to specify @XmlElement(nillable=true) in your field / property to get the following:

 @XmlElement(nillable=true) private String foo; 

Creating from an XML Schema

Below I will demonstrate how to generate this mapping if you look at the XML schema.

XML Schema (schema.xsd)

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="foo"> <xs:complexType> <xs:sequence> <xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0" /> <xs:element name="myIntElementName" type="xs:int" nillable="true" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 

Why do you get a property of type JAXBElement

A JAXBElement type property is created in your model because you have an element that has nillable, this is minOccurs="0" . Using JAXBElement allows the model to distinguish between the missing element (the property is null) and the presence of an element with nil="true" (JAXBElement with the set nil flag set).

 <xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0" /> 

External Binding File (binding.xml)

An external binding file can be specified to tell the JAXB implementation not to generate any JAXBElement type JAXBElement . Please note that this will not allow JAXB to crawl all XML documents.

 <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"> <jaxb:bindings> <jaxb:globalBindings generateElementProperty="false"/> </jaxb:bindings> </jaxb:bindings> 

XJC call

Below is an example of how to use an external bind file from an XJC call

 xjc -b binding.xml schema.xsd 

Generated Model (Foo)

The generated model will look something like this:

 import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "myStringElementName", "myIntElementName" }) @XmlRootElement(name = "foo") public class Foo { @XmlElement(nillable = true) protected String myStringElementName; @XmlElement(nillable = true) protected Integer myIntElementName; public String getMyStringElementName() { return myStringElementName; } public void setMyStringElementName(String value) { this.myStringElementName = value; } public Integer getMyIntElementName() { return myIntElementName; } public void setMyIntElementName(Integer value) { this.myIntElementName = value; } } 

Additional Information

+4
source share

All Articles