Recently I worked on one project which involved using JAXB to generate XML message fro the excel file. Some data in the file are financial amount and tax amount. When it is read from the excel it is represented as the floating number. However in the generated XML message these data occurs to have something like the below:
In schema GSTAmount is defined as xsd:decimal and the rounding is done before marshaling by JAXB. I did some serach using Google and learned some tips when dealing with financial amount in Java.
Use BigDecimal rather than double when defining the financial amount.
"The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate. " However it is quite slow and expensive to create. It is wise to reuse the existing instance whenever the message of the same schema is repeated for marshalling or unmarshalling.
One way is to create single JAXBContext with many JAXB objects that are used and then share this JAXBContext within the application. JAXBContxet has one factory method to instantiate JAXBContext. This factory method can take in multiple JAXB classes into the context. There are two forms of instantion of JAXBContext as shown in below.