How to make JAXB parse scientific notation for BigInteger?

I'm kind of new to when it comes to JAXB and marhsalling / unmarshalling, and I'm struggling to do my job to get things working.

So far I have written my XSD and generated my POJOs using xjc. All of this works great. Also, when I need to turn off data that comes in as String data or a w3c document, I use JAXBContext.generateSchema () to create the appropriate schema for the expected class that I get. All of this works great.

My problem arises when I analyze the circuit generated by generateSchema (). According to JAXB docs , BigDecimal is generated as decimal type xsd.

My input sometimes contains scientific notation (ex: 1.12E12). When I try to untie this using the JAXB generated circuit, I get a javax.xml.bind.UnmarshalException exception:

[org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '1.32400146520976E-4' is not a valid value for 'decimal'.] 

Is there a way to correctly parse scientific notation when using the "decimal" type or should it be "double"?

Initially, I thought that I could use double representation, since double can analyze scientific notation, however, I am dealing with financial values ​​(this is for bank calculations), and therefore, I realized that using double representation will cause rounding errors due to it inherent limitations.

So, I think the question is how to create custom bindings that allow me to analyze scientific notation and store data in BigDecimal? I looked at XMLAdpater , but I don't understand how to use it for the case I'm looking at. If anyone can give a presentation or an example (other than the ubiquitous map example that I continue to search on the Internet), I would greatly appreciate it.

Thanks!

Eric

+4
source share
1 answer

1) NO user bindings NO, as the XML Schema specification is pretty clear for lexical decimal notation .

2) I think tuning is the only way to overcome this; another way maybe ?

What would bother me is that if someone tries to use the generated XSD to validate instances of XML instances, it will fail even now if you don't have a scheme that allows both ... Although shunned , xsd: union may be the only way out (you still have to do custom binding) ...

+2
source

Source: https://habr.com/ru/post/1393844/


All Articles