March 28, 2012

Castor - Old way of converting XML to Java and vice-versa

This is famous but little older than Jibx.

How to work with Castor?

It has two ways of implemention, first one as expected using mapping file and the other one is using ClassDescriptors in Castor API.
In First implementation, we will have the following code.
For marshalling:

    StringWriter stringWriter = new StringWriter();
    Marshaller marshaller = new Marshaller(stringWriter);
    marshaller.setMapping(mapping);
    marshaller.marshal(object);

For unmarshalling :

    StringReader stringReader = new StringReader(xmlString);   
    Unmarshaller unmarshaller = new Unmarshaller(mapping);
    Object unmarshalledObject = unmarshaller.unmarshal(new InputSource(stringReader));

Second Implementation as follows,

For Marshalling:
Marshaller.marshal(this, out);


For Unmarshalling:
(Clazz) Unmarshaller.unmarshal(Clazz.class, reader);

No need of mapping, but Need a Descriptor File which extends org.exolab.castor.xml.util.XMLClassDescriptorImpl and define descriptors for fields in that file.


At the end Castor & Jibx works fine and looks similar. But in performance Jibx is far better than the other, Since it uses pull parsing, where as Castor uses SAX Parser as it is slower in performance.


Jibx - Translates Xml to Java and vice versa.

I have been using Jibx since 2 years, which is basically a XML to JAVA and vice-versa Conversion Tool.

It's very simple to use just like the POJOs in our Java Code.

Instructions to use:
Method 1:
We can generate Binding file (mapping file), Schema from Code itself using BindGen tool of Jibx. Otherwise,
  • Write your POJOs you want to convert to XML.
  • Write a mapping file(xml) which tells what fields in that POJO exactly mapped to the XML tags (this is well described in Jibx )
  • Build the mapping file, then the POJO is ready to be used, to convert into XML. 
Method 2:
We can generate Code and Binding file (mapping file) from  schema (XSD) using CodeGen tool. Otherwise,
  • Write your POJOs you want to convert to XML.
  • Write a mapping file(xml) which tells what fields in that POJO exactly mapped to the XML tags (this is well described in Jibx )
  • Build the mapping file, then the POJO is ready to be used, to convert into XML and vice-versa.
Happy conversion....  :)

December 21, 2010

Excel to Java Using POI libraries

My goal is to convert Excel data to Java Objects ( to our own POJOs).

There are two types of Excel versions:
1) Excel 97 - 2003 (*.xls ) and
2) Excel 2007 (*.xlsx) .

Libraries (jars) used are :
poi-3.6.jar for type 1,
poi-ooxml-3.6.jar for type 2.
These are from apache, we can download them from here

Code for 1st type:


try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileNameWithPath)); // fileNameWithPath should be path to the '.xls' file.
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
if(sheet != null)
{
int first = sheet.getFirstRowNum();
int last = sheet.getLastRowNum();
first += 1; //Ignore first 1 row - they are headers
for(int i= first; i<=last; i++)
{
row = sheet.getRow(i);
if(row != null && row.getCell(COLUMN_CITY_NAME) != null)
{// COLUMN_CITY_NAME is an integer (actually starts with 0 )
/*code goes here to retrieve the cell information using row.getCell(colNum); */
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}


Code for 2nd Type:


try {
Workbook wb = new XSSFWorkbook(fileNameWithPath); // fileNameWithPath should be path to the '.xlsx' file.
Sheet sheet = wb.getSheetAt(0);
Row row;
if(sheet != null) {
int first = sheet.getFirstRowNum();
int last = sheet.getLastRowNum();
first += 1; //Ignore first 1 row - they are headers
for(int i= first; i<=last; i++)
{
row = sheet.getRow(i);
if(row != null && row.getCell(COLUMN_CITY_NAME) != null){
// COLUMN_CITY_NAME is an integer (actually starts with 0 )
/*code goes here to retrieve the cell information using
row.getCell(colNum);
*/
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

August 16, 2010

Tutorial on Spring Web MVC - Nice one!!!

I have found a nice tutorial on spring MVC. It is very useful for newbie in spring. Get on to it through the following link. However, I will be posting Simple web application configured in Jetty (Web Server) soon...
click this link to go the tutorial