¿Cómo hacer que JAXB analice la notación científica para BigInteger?

I'm kind of a novice when it comes to JAXB and marhsalling/unmarshalling and have been struggling through my implementation to get things working.

So far, I've written my XSDs and generated my POJOs using xjc. All that works fine. Additionally, when I need to unmarshall data coming in as String data or a w3c document, I use JAXBContext.generateSchema() to produce the matching schema relating to the expected class that I am receiving. All that works fine as well.

My problem comes when I analyze the schema that generateSchema() produces. As per the JAXB docs, a BigDecimal is generated as an xsd decimal type.

My input sometimes contains scientific notation (ex: 1.12E12). When I try to unmarshall that with the schema generated by JAXB, I get an 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 some way to properly parse scientific notation when using a "decimal" type or must it be "double"?

Originally, I thought I could use a double representation as double's can parse scientific notation, however, I am dealing with financial values (this is for banking calculations) and consequently, I realized that using a double representation will cause rounding errors due to it's inherent limitations.

So I guess the question becomes how to create custom bindings that would allow me to parse scientific notation and store the data in BigDecimal? I've looked at XMLAdpater, but I do not understand how to use it for the case I'm looking at. If anyone can provide insight or an example (other than the ubiquitous Map example I keep finding online) I would greatly appreciate it.

¡Gracias!

Eric

preguntado el 30 de enero de 12 a las 19:01

1 Respuestas

1) Without custom bindings, NO, since the XML Schema spec is pretty clear on what the lexical representation of a decimal .

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

What I would be concerned though is that if someone would be trying to use the generated XSD to validate instance XMLs, that will fail now and then unless you have a schema that allows for both... Although rechazados, xsd:union might be the only way out (you still have to do a custom binding)...

Respondido el 31 de enero de 12 a las 00:01

I was somewhat expecting as much. After further reflection, I realized that storing the data internally as a double wouldn't even work, so I definitely need custom work, but not sure exactly what that entails. I pensar it is using XMLAdapter, but not sure. - Eric B.

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.