Java and JAXB tutorial – unmarshalling

Q. What does JAXB stand for? What is an XML Binding?
A. JAXB means Java API for Xml Binding. XML binding maps an XML to in-memory Java objects. The principle advantage of using JAXB when marshaling and demarshaling XML is that is simplifies the programming model by allowing us to simply annotate a few POJOs and use the JAXB API’s and you can serialize to XML and deserialize from XML very easily. This makes it much simpler than alternatives such as DOM and SAX.

Q. Why use JAXB?
A. JAXB is a reference interface for which you can have a number of different implementations like default reference implementation shipped JDK6, JaxMeAPI, MOXy, Metro, etc. JAXB is available in JDK6 onwards, so it doesn’t require any external library and it doesn’t require a XML schema to work. XML schema is optional. Easy to use as you can use annotations on your model classes. It is supported by RESful frameworks like Jersey, Spring MVC, RESTEasy, etc.

Here is an example of Unmarshalling XML to Java object:

Step 1: Sample XML to unmarshall

Step 2: The in memory Java object class. It requires a default constructor.


Step 3: The JAXB unmrshaller utility class that can be used with different object types and XML string.

Step 4: The test class demomnstrating the unmarshalling (i.e. convert XML string to Java object) in action.


Step 5: Finally, the output from the toString( ) method in class Employee.


300+ Java Interview FAQs

Tutorials on Java & Big Data