Zero double value
I am using an xsd schema with the data type of a double
element as follows:
<xsd:element name="value" type="xsd:double"/>
I am using jaxB unmarschaller to create a java class with the appropriate object and attributes. The result is as follows:
protected double value;
Now xml data can send elements with a null value, but I do not enter the position to match the xsd schema for the Double.class data type. Is it possible to overwrite an attribute in a java class?
If you cannot modify the XML schema to make the value element nillable, you can do the following using an external JAXB binding file:
External Binding File (binding.xml)
You can use an external binding file as shown below:
<jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" version="2.1"> <jaxb:bindings schemaLocation="double.xsd"> <jaxb:bindings node="//xs:element[@name='value']"> <jaxb:property> <jaxb:baseType name="java.lang.Double"/> </jaxb:property> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings>
XML Schema - double.xsd
The above binding file applies to the XML schema, which looks like this:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="value" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
XJC challenge
xjc -d out -b binding.xml double.xsd
Generated class
package generated; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = {"value"}) @XmlRootElement(name = "Root") public class Root { @XmlElement(required = true, type = Double.class) protected Double value; public Double getValue() { return value; } public void setValue(Double value) { this.value = value; } }