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;
}
public class Person
{
public int customerNumber;
}
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.
1 comment:
Hello Rajesh. Excellent article on JiBx. Please share your inputs on advance jibx topic.
Post a Comment