XML binding using JiBX

JiBX is a XML binding framework which binds XML to your java class.
XML binding is a 2 phase process:
1. JiBX uses binding definition to define the rule how conversion wil happen and modifies byte code of the class.
2. In phase 2 JiBX runtime does marshalling and un-marshalling of java objects to populate XML to java and java 2 XML.

Algorithm
  • Add following jar files to your lib directory from JiBX distribution
    1. bcel.jar (Byte Code Engineering Library used to change bytecode for the class)
    2. jibx-bind.jar (1,2 are used in phase I)
    3. jibx-run.jar
    4. jibx-extras.jar
    5. xpp3.jar (3,4,5 are used in phase II)
  • Modify java class using binding compiler (java -jar ../lib/jibx-bind.jar binding.xml)
  • Write code for marshalling and unmarshalling of java object

Example:



public void XML2Java2XML() {
try {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

Object obj = uctx.unmarshalDocument
(new FileInputStream("customer.xml"), null);
Customer customer = (Customer)obj;
System.out.print(customer.toString());

IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(4);
mctx.marshalDocument(obj, "UTF-8", null,
new FileOutputStream("customer2.xml"));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JiBXException e) {
e.printStackTrace();
}
}

3 comments:

Francy said...

Hi!
Nice Blog!

XML is well suited for the interchange of data, since XML documents are self-describing, easily parsed and can represent complex data structures. Also, there is a wide variety of high-quality, inexpensive tools for parsing and transforming XML documents. When using XML for data interchange, ideally, the sending application will be able to export an xml conversion document, and the receiving application will be able to import an XML document. Unfortunately, many legacy applications use flat files to import or export data. So, companies will need to convert flat files into XML documents when sending data to XML-capable applications. Likewise, companies will need to convert XML documents into flat files that can be imported into legacy applications.

pravesh said...

JIBX is really faster other XML to object marshalling/un-marshalling technologies.

There is only one issue which is debugging JIBX applications becomes tougher because of only one reason; the JIBX modifies the java beans (POJO) .classes and puts the marshalling/unmarshalling code right into the bytecode (This is what makes it faster), which becomes difficuly to catch various exception which would occur otherwise.

Also there is less documentation available on the internet, which makes adopting to JIBX even tougher.

I personally struggled to implement JIBX in a webservice based application. But once it was done then JIBX really rocked :)

Anesha said...

Hi Nice Blog. We Offer a wide range of back office and I.T Enabled XML Conversion Which match the high global standards in terms of efficiency and accuracy.