Showing posts with label JAXB. Show all posts
Showing posts with label JAXB. Show all posts

Monday, July 15, 2013

BigNumber vs Double

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:


       <GSTAmount>26.789999999999999999999999999967899</GSTAmount>


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.


  1. Use BigDecimal rather than double when defining the financial amount.
  2. When using BigDecimal use its string conductor.
Here is the excellent link which explains this issue very clearly. How to Use Java BigDecimal: A Tutorial

Wednesday, March 2, 2011

JAXBContext is expensive to create

According to Java Doc

"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.

JAXBContext jc = JAXBContext.newInstance("com.toic.order.foo:com.toic.order.bar" );

JAXBContext jc = JAXBContext.newInstance(com.toic.order.foo.class, com.toic.order.bar.class" );

Where com.toic.order.foo and com.toic.order.bar are the qualified class names of two JAXB classes that are taken into the JAXB context.








Wednesday, February 2, 2011

Creat a JAXB class library in NetBeans IDE









It's convenient to have a seperate JAXB class libray project when using JAXB in processing XML in Java application.

The below are the some simple steps to demonstrate how to create such a project in NetBeans IDE.

1. Create the project of Java class library
  • Start a new project by selecting New Project... from File menu
  • Choose project type as: Java Class Library
  • Give the name of the project and then click on Finish button

2. Create the JAXB binding
  • Right click on the new created project and choose New
  • Choose the file category as: XML and File Types as: JAXB Binding
  • Give the name of JAXB Binding
  • Select the schema file

3. Build the project


init:

deps-clean:

Deleting directory D:\WIS\commbiz-jaxb-lib\build

clean:

init:

deps-jar:

xjc-typedef-target:

jaxb-code-generation:

Created dir: D:\WIS\commbiz-jaxb-lib\build\generated\addons\jaxb

Created dir: D:\WIS\commbiz-jaxb-lib\build\generated\jaxbCache

Created dir: D:\WIS\commbiz-jaxb-lib\build\classes

Created dir: D:\WIS\commbiz-jaxb-lib\build\generated\jaxbCache\Commbiz

Compiling file:/D:/WIS/commbiz-jaxb-lib/xml-resources/jaxb/Commbiz/combiz_alterting_service.xsd

Writing output to D:\WIS\commbiz-jaxb-lib\build\generated\jaxbCache\Commbiz

Copying 54 files to D:\WIS\commbiz-jaxb-lib\build\generated\addons\jaxb

Compiling 54 source files to D:\WIS\commbiz-jaxb-lib\build\classes

compile:

Created dir: D:\WIS\commbiz-jaxb-lib\dist

Building jar: D:\WIS\commbiz-jaxb-lib\dist\commbiz-jaxb-lib.jar

jar:

BUILD SUCCESSFUL (total time: 2 seconds)

All generated JAXB classes based on the given schema file are in the .jar file.