Brook Preloader


Marshalling and UnMarshalling in JAXB


JAXB abbreviation “Java Architecture for XML Binding”.JAXB is a software framework that allows “Jakarta EE” developers to map Java classes to XML.

What is JAXB?

Provides API, tools, and a framework that automate the mapping between XML documents and Java classes. Provides compiler that compiles XML schema to Java classes. The JAXBContent class provides the “client’s entry” point to the JAXB API. It provides API for Marshalling, UnMarshalling, and validating.JAXB-API is having two versions I .e.., 1.x and 2.x versions.

The below figure as shown as the Architecture of JAXB.

Image for post
JAXB Architecture

Converting of Java Object into XML.

Converting of XML into Java Object.

The need for JAXB:

when we design the Webservice application taking the request in the form of XML and giving the response of XML. so in this case the request of XML will convert into java object and whatever response is created by java Object will convert into XML.

Steps to Build the Application:

Step1: Create a Binding class(Java Bean based class)

Step2: Define the mapping between Java programming elements to XML. (The Mapping we can define by using JAXB Annotations)

Step3: Writing the logic for Marshaling/UnMarshalling as per requirement.

Project Structure:

The below figure shows the final structure of the project

Image for post

Creating the Project:

Open SpringBoot app Go to File -> New ->Select the ->Spring Starter web project then click on Next and Finish as shown in the below figure.

Image for post
Project Structure

Step1: Create a Binding class

Binding class is nothing but Java Bean Based class. Now create one binding class i.e., entity or POJO class and it is named “”.

Image for post

After entering the fields we should use the getter and setter for all the fields. For the Entity class, we use a method called “Serializable” condition. In this, we use the method called “@XmlRootElement” annotation. In that annotation name is optional.

@XmlRootElement is an annotation and the purpose is to uniquely associate a root element with a class. Since JAXB classes map to complex types, it is possible for a class to correspond to multiple root elements.

Step2: Define the mapping between Java programming elements to XML.

The Mapping we can define by using JAXB Annotations. Now we are going to implement the Java Object into XML.

Image for post
Java object to XML Data

After that, we are going to create the JAXBContext object. To create JAXBContent object we can use newInstances() from JAXBContent class.The newInstances() is a static factory method.

->JAXBContext context = JAXBContext.newInstance(employee.getClass());

-> JAXBContext context = JAXBContext.newInstance(Employee.class);

As we show in the above JAXBContext, we can use any of this method both are same methods.In this we can observe that we are using “newInstance” it is “paramistered” method.The newInstances method is taking “java.lang.class” object as “param”.If we want to convert Employee data with Your XML.

Image for post

So as we showed in the above figure it is asking to implement the “JAXB Exception”.so we use the Add throws declaration Exception.

Image for post

Now we are going to create the Marshaller object from JAXBContent.

Image for post

Marshaller is one of the interfaces, it implements class object return by the “createMarshaller” method.

Image for post

After compiling the code, the output will display as shown in the below figure. The output will be display in the console.

Image for post
Displaying the output in the console

call marshall from Marshaller Object. If we want to convert object into XML then we can the marshall method. For the marshall method, we can call the two parameters.

Image for post

We want to pass any elements, those elements can pass at “Object JAXB element”.For example, if we convert to Employee object into XML we can pass here and also “Filename” at the second parameter.

what is the need to keep the File here?
Is the given object is converting into XML after converting the given object into XML, the XML we want to store in which file so for that we can pass at file output. Store the generated the XML in a file

Image for post

After this execute the that the output shows on the console as “Successfully Done” and the file will be generated as shown in the below figure.

Image for post

So the employee.xml will display the data as shown in the below figure as

Image for post
employee.xml File Data

The generate XML we can also store in one String object. For that String object, we are going to use the “String Writer Object”. To get generate XML in the form a string by using string Writer.

Image for post
String Writer Object

So the output will displays in the console as shown in the below figure.

Image for post
The output of String Writer Object.

Marshaling operation it is possible to display in the command prompt and to store in the file, and also getting the file in String.

Comparing all of these which one is better?

Getting the File into the String is better because, If we want to call the one WebService means if we want to call one web service provider application in the form XML.

Advantages JAXB:

  • It is simpler than SAX or DOM parser.
  • No need to be aware of XML parsing techniques.
  • No need to access XML in a tree structure.

Disadvantages JAXB:

  • It is a high layer API, so it has less control over parsing as compared to SAX or DOM.
  • It is slower than SAX.
0 0 vote
Inline Feedbacks
View all comments