Introduction To JAXB

Jan 25

This is the perfect place to start learning about JAXB even if you don’t have any knowledge about this topic. First of all JAXB stands for Java Architecture for XML Binding which is an easy way to convert your Java objects to XML and vice versa. JAXB is a part of Java SE 6 API so you don’t need anything except eclipse and this page. Before starting you need to know couple of expressions:

  • Marshaling which is the process of encoding Java objects to put them on XML representation.
  • Unmarshaling which is the process of decoding theΒ  XML to reconstruct Java objects.


Lets start, create a Java project and give it a name (I’m going to name mine LearningJAXB). I will keep this as simple as I can so let’s start with a Human class:

you may notice that I annotated the class with @XmlRootElement and this is what is needed to make the XML representation for this class. Next step is to create our Main class to start marshaling process:

The above code will generate this XML in the console:

Now, how to put that XML generated code in a file to use it in unmarshaling process? Actually this is so easy and all you need to do is to make the previous code looks like this:

You will find a file named jaxb-output in your project directory which contains the XML representation. Next step is trying to unmarshal the object from jaxb-output file. This will be done as the following (I will create a new class Main2 for the unmarshaling purpose) :

The output of the previous code will be:

Which are the same values used before (obviously).

Now let’s make this a little more complicated. I will create another two classes “Employee” which inherit from Human and “Department” which will be composed inside Employee. Department class will be like this:

As you see I didn’t put any annotations at all because this class is not a root element. Employee class:

Now let’s modify our Main class in order to marshal an employee not a human:

The jaxb-output file will contain this now:

As you see all the inherited properties and department properties are there. Finally if you need to exclude one property from the XML all you need is to annotate that property with @XmlTransient.

The project I created or this tutorial is Here

I hope this will help you to start your XML binding πŸ™‚

13 comments

  1. Thanks for the valuable information about JAXB, it is one of the better ones I have came across.
    Your whole blog are really valuable to most Developers.

    Good Luck

  2. This web site can be a walk-through for all of the info you wanted concerning this and didn’t know whom to ask. Glimpse here, and you’ll definitely learn it.

  3. I’m getting a difficult time obtaining the best theme, no cost or paid, for my web page. So I’m curious to understand what themes are you currently working with .Thanks in advance

  4. As a web site owner I believe the subject matter here is reallyfantastic. I appreciate it for your hard work. You should keep it up forever! Good Luck…

  5. Good post however , I was wondering if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit further. Cheers!

  6. Sweet internet site , super style and design , very clean and apply friendly .

  7. Thanks for the valuable information about JAXB,

Leave a Reply