Thursday, October 30, 2008

Jibx Tutorial

Sample JIBX application for beginners

No enterprise can have all the applications being developed and run in single platforms. Different applications needs to communicate each other also. XML is the common standard of communication between hetrogenous systems. XML has it's own structure and applications consuming XML must be fast enough to marshall(data structure to XML) and unmarshall(XML to data structure) very fast.

JIBX is one of the famous framework used in Java community for converting XML to java classes and vice versa. This is one of the technology commonly used in my current projects. In this session, I am interested to share how a beginner can start learning JIBX using a sample application.


1. Download jibx package from the site http://sourceforge.net/projects/jibx/

2. For this tutorial, we only need jar files located in the lib folder of this jar.

3. Extract those jars mentioned in step 2 to c:\study\jibx\lib

4. Create a binding.xml file with the following content and copy it in to the folder
c:\study\jibx\schemas

<binding>

<mapping name="customer" class="Customer">

<structure name="person" field="person" >

<value name="cust-num" field="customerNumber"/>

</structure>

<value name="street" field="street"/>

</mapping>

</binding>

5. Create Customer.java class as given below


public class Customer
{
public Person person;
public String street;
}

6. Create Peson.java class as given below

public class Person
{
public int customerNumber;
}

7. Create a program called Tester to execute the code as given below.



import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import org.jibx.runtime.BindingDirectory;

import org.jibx.runtime.IBindingFactory;

import org.jibx.runtime.IMarshallingContext;

import org.jibx.runtime.IUnmarshallingContext;

import org.jibx.runtime.JiBXException;

public class Tester {

public CustomerManager() {

try {

IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);

IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

Object obj = uctx.unmarshalDocument (new FileInputStream("C:/study/jibx/customer.xml"), null);

Customer customer = (Customer)obj; System.out.print(customer.street);

IMarshallingContext mctx = bfact.createMarshallingContext();

mctx.setIndent(4);

mctx.marshalDocument(obj, "UTF-8", null, new FileOutputStream("C:/study/jibx/customer2.xml"));

}

catch (FileNotFoundException e){

e.printStackTrace();

}

catch (JiBXException e){

e.printStackTrace();

}

}




public static void main(String[] args) {

new CustomerManager();

}

}

8. Run ant process for making the executablesa.

a. Pls make sure that ant is installed in your system.

b. Use the following build script to build and execute the main class Tester.java

<!-- <project name="Make_Jibx_Project" basedir=".">
<property name="lib_dir" value="c:/study/jibx/lib"><property name="mapping_files_loc" value="c:/study/jibx/mappings">
<path id="compile_classpath">

<pathelement location="${lib_dir}/jibx-run.jar">

<pathelement location="${lib_dir}/xpp3.jar">

<pathelement location=".">

</pathelement>

<path id="bind_classpath">

<pathelement location="${lib_dir}/jibx-run.jar">

<pathelement location="${lib_dir}/xpp3.jar">

<pathelement location=".">

</pathelement>
<!-- JiBX binding compiler task definition -->
<taskdef name="bind" classpath="${lib_dir}/jibx-bind.jar" classname="org.jibx.binding.ant.CompileTask">

<!-- ************************* --><!-- Compile the entities-->
<target name="compile-entities">

<javac includes="**/*.java" destdir="." srcdir=".">

<classpath refid="compile_classpath"></classpath>

</javac>
<!-- ************************* -->

<!-- Bind the entities-->

<target name="bind-entities" depends="compile-entities"><bind><classpath refid="bind_classpath"><bindingfileset dir="${mapping_files_loc}">

<include name="*.xml">

<include name="**/*.xml">

</include></include>

<java classname="Tester">

<classpath refid="bind_classpath">

</classpath>

</java>
</bindingfileset>-->

</classpath>

</bind>

</target></target>

</taskdef>

c. Put the customer.xml given below in c:\study\jaxb folder

<customer>

<person>

<cust-num>

123456789

</cust-num>

</person>

<street>

12345 Happy Lane

</street>

</customer>

d. Go to directory c:\study\jibx and give the command ant -noclasspath bind-entities

9. If every thing goes fine you will get out put

1234 Happy Lane and a new xml cutomer2.xml in c:\study\jaxb foler


I will write advanced Jibx tutorial next time which shows the features in JIBX which is used in realt time products of enterprise applications.