Friday, 15 March 2013

java - JAXB marshalling for BigDecimal using fractionDigits -



java - JAXB marshalling for BigDecimal using fractionDigits -

so here's problem. i'm given xsd generated xml file should comply. using org.apache.cxf.cxf-xjc-plugin maven plugin , external binding file generate source code. when i'm trying marshall object generated xml doesn't meet requirements.

my xsd contains following:

class="lang-xml prettyprint-override"><xsd:element maxoccurs="1" minoccurs="0" name="amount"> <xsd:simpletype> <xsd:restriction base="xsd:decimal"> <xsd:totaldigits value="13" /> <xsd:fractiondigits value="2" /> </xsd:restriction> </xsd:simpletype> </xsd:element> ... <xsd:element maxoccurs="1" minoccurs="0" name="rate"> <xsd:simpletype> <xsd:restriction base="xsd:decimal"> <xsd:totaldigits value="8" /> <xsd:fractiondigits value="5" /> </xsd:restriction> </xsd:simpletype> </xsd:element>

and generated piece of xml looks this:

class="lang-xml prettyprint-override"><amount>109.5</amount> ... <rate>10.25</rate>

while expecting be:

class="lang-xml prettyprint-override"><amount>109.50</amount> ... <rate>10.25000</rate>

is there way solve problem in clean way?

i prefer not writing several adapters every single totaldigits, fractiondigits combination. , xsd subject alter i'd leave generated source code untouched.

you need utilize xmladapter utilize case. below sample binding file help generate them. logic contained in decimalformatter class contained methods different required formats.

class="lang-xml prettyprint-override"><jxb:bindings xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jxb:bindings schemalocation="schema.xsd"> <jxb:bindings node="//xs:element[@name='amount']"> <jxb:property> <jxb:basetype> <jxb:javatype name="java.math.bigdecimal" parsemethod="org.example.decimalformatter.parsedecimal" printmethod="org.example.decimalformatter.printdecimal_2places" /> </jxb:basetype> </jxb:property> </jxb:bindings> <jxb:bindings node="//xs:element[@name='rate']"> <jxb:property> <jxb:basetype> <jxb:javatype name="java.math.bigdecimal" parsemethod="org.example.decimalformatter.parsedecimal" printmethod="org.example.decimalformatter.printdecimal_5places" /> </jxb:basetype> </jxb:property> </jxb:bindings> </jxb:bindings> </jxb:bindings>

for more information

http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html

java xsd jaxb xjc cxf-xjc-plugin

No comments:

Post a Comment