Quick Tutorial on JAXB

JAXB is an acronym for Java Architecture for XML Binding. Well, idea of this post is not to get into the details of what is JAXB and what does it do? There are lot of good articles floating on net for the same. I too have provided few good references at the bottom of this post. Soul focus of this post is to present a working example and let you know of three easy steps to start working with JAXB. Trust me, it is really one of those good tools when you can’t resist to say “Vow, it is that easy and awesome”.

I am assuming that you only have an xml file and you want to digest it in your code. During early days we used to use DOM and SAX parsers to parse the xml file and read it’s contents. Beauty of JAXB is that it presents your xml file as Java Classes. Did I say Java, yes that’s right. All you need to do to access xml file content is to have a corresponding xml schema for your xml file and compile it using the JAXB compiler to create Java Classes (For reference, a schema is an XML specification or rule that governs the allowable component of an XML document and the relationships between the components). XML File conversion to Java Classes takes place as shown below:

XML File -> XML Schema -> JAXB Compiler -> Java Classes

Now, let’s get into the code. You may download the source code from jaxb-poc.

Here is how the students.xml file look:

Step 1: Create a corresponding XML Schema

Like I mentioned above, first you need to create an XML schema of the xml file you have. You can either create it manually or you can use some tool to do that. I used Altova XmlSpy. You may download it’s fully functional 30-days free trial version from here. Sometimes, you can’t rely on the tool completely and you many need to modify the automatically generated xsd (XML Schema Document) file a bit.

Here is how students.xsd created by using XmlSpy looks like:

Step 2: Create Java Classes

Now we will create Java Classes using student.xsd created in step 1. We will use “xjc” i.e. JAXB Compiler to create Java Classes. Prior to Java 5, there was a need to seperately download JAXB. But with Java 5 and beyond, the jars file required to support JAXB comes bundled with the package. So if you have java 5 and beyond, you are good to go with JAXB without any separate installations or downloads.

Open the command prompt and go the project directory. Run “xjc” command as below:

JAXB xjc Command

Here -p is for the package under which java classes are to be created and -d is for the directory under which “com.jaxb.student” package is to be created.

Below image depics the file structure after creation of java classes:

JAXB Project File Structure

Step 3: Accessing XML Content

All we need to do now is to play with Java Classes created in step 2 above. Here is the java code snippet that shows how various attributes and parameters of students.xml have been accessed using java classes:

When you run JaxbClient, below will be the output:

JAXB Client Output

Hope you find the tutorial helpful. You may look into below articles for further references:

http://www.oracle.com/technetwork/articles/javase/index-140168.html

http://www.roseindia.net/jaxb/r/jaxb.shtml

Feel free to leave you footprints in the comments section below for any feedback, queries or suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">