Java JaxB Generation, How do I get BigDecimal from my xsd?

I have an xsd annotation which I am trying to get to the marshal in a java object. I would like java to end up with BigDecimal for its meaning. What can I type in xsd to do this? I am using xjc ant task

<xjc schema="my.xsd" destdir="generated" header="false" extension="true" /> 

Here is the corresponding xsd -

 <complexType name="Size"> <attribute name="height" type="BigDecimal"></attribute> <!-- this is wrong--> </complexType> 

I would like to end this with the generated class -

 public class Size { @XmlAttribute(name = "height") protected BigDecimal height; } 
+8
java xml xsd jaxb bigdecimal
source share
2 answers

A JAXB (JSR-222) will generate java.math.BigDecimal from the decimal type (see table 6-1 in JAXB 2.2).

XML Schema (schema.xsd)

 <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified"> <element name="foo"> <complexType> <sequence> <element name="bar" type="decimal"/> </sequence> </complexType> </element> </schema> 

XJC challenge

 xjc schema.xsd 

Java Model (Foo)

 package org.example.schema; import java.math.BigDecimal; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = {"bar"}) @XmlRootElement(name = "foo") public class Foo { @XmlElement(required = true) protected BigDecimal bar; ... } 
+8
source share

I get it. The answer is to use the binding.xjb class

binding =

 <jxb:javaType name="java.math.BigDecimal" xmlType="xs:decimal"/> 

ant -

 <xjc schema="my.xsd" destdir="generated" binding="myBinding.xjb" header="false" extension="true" /> 

xsd =

 <attribute name="height" type="decimal"></attribute> 

This means that everything marked as decimal numbers will turn into a large decimal number, but in my case this is normal. Hope this helps someone else.

+1
source share

All Articles