So here's my problem. I'm given an XSD to which my generated XML file should comply. Using the org.apache.cxf.cxf-xjc-plugin
maven plugin and an external binding file I generate the source code. But when I'm trying marshall my object the generated XML doesn't meet my requirements.
My XSD contains the following:
<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 the generated piece of XML looks like this:
<amount>109.5</amount>
...
<rate>10.25</rate>
While I was expecting it to be:
<amount>109.50</amount>
...
<rate>10.25000</rate>
Is there a way to solve this problem in a clean way?
I would prefer not writing several adapters for every single totalDigits
, fractionDigits
combination. And as the XSD is subject to change I'd like to leave the generated source code untouched.
I'm confused as to what could possibly be gained by maintaining the trailing zeros.
If the trailing zeros are that important to preserve, then use a string value instead of a number and use attributes to specify its width and decimals.
In any case, the trailing zeros will never impact calculations using any of these values, and the only way you would be able to present them is by converting the results into a string and padding it yourself. For some help doing that, you may want to see...
CXF client SOAP message formatting
I've created some special XmlAdapter, for example
then i add the XmlAdapter in the properties:
Then, do the same thing with 2 decimals adapeter.
If you can change the XSD to use the
precisionDecimal
type documented here you might be able to use theminScale
andmaxScale
facets set to the same value.You will need to use
XmlAdapter
for this use case. Below is a sample binding file that will help you generate them. The logic would be contained in aDecimalFormatter
class that contained methods for all the different required formats.For More Information