Using JAXB for XML with Java

Using JAXB for XML with Java


Java Architecture for XML Binding (JAXB) is a library that helps to bind XML schemas and Java representations. JAXB provides you with a mechanism to marshal Java objects into XML and the other way around – unmarshal XML into Java objects.

XML is an industry standard for defining the contents of your message. XML along with Java are complementary technologies to exchange data across the Internet. When you work with XML, you need a way to take an XML file, and then convert it into some sort of data structure, which your program can manipulate. You also need to serialize the state of your Java objects into XML. JAXB is one library that performs such marshalling and unmarshalling operations.

In this post, I will discuss on how to marshal Java objects into XML and the other way around using JAXB.

Using JAXB to Marshall Java Objects to XML

Let us consider a scenario where we need to model a product for an e-commerce store. Apart from fields, such as product Id, description, image URL, and price, a Product object is also composed of a User object that represents the user who adds the product to the store.

Our intent is to marshal the Product object, along with its composed User object into XML by using JAXB.

We can start by creating a User POJO to model the user.

User is a simple POJO with the id, name, and email fields, along with their corresponding getter and setter methods.

Next, we will create the Product POJO. While creating this POJO, we will use annotations introduced in JAXB 2.0 to control how our Product object is marshalled to XML.

Note: As from JDK 1.6, JAXB is bundled with JDK. Therefore you don’t need to add any dependency for it.

The code of the Product POJO is this.

In this Product class, we used a number of JAXB annotations. They are:

  • @XmlRootElement: This annotation is used at the top level class to indicate the root element in the XML document. The name attribute in the annotation is optional. If not specified, the class name is used as the root XML element in the document.
  • @XmlAttribute: This annotation is used to indicate the attribute of the root element.
  • @XmlElement: This annotation is used on the properties of the class which will be the sub-elements of the root element.

The Product POJO is now ready to be marshalled into XML. To do so, let us write a JUnit test class. If you are new to JUnit, I suggest going through my Unit Testing with JUnit series.

The test class is this.

In this code:

  • Line 19 – Line 24: Instantiates and initializes a User and a Product object in the setup() method marked as @Before.
  • Line 33: The JAXBContext.newInstance() method obtains a JAXBContext for the Product class which you want to marshal. JAXBContext provides the entry point for the JAXB API.
  • Line 34: Creates a Marshaller through a call to the createMarshaller() method of JAXBContext. In JAXB, the Marshaller class governs the process of marshalling Java objects into XML data through its various marshalling methods.
  • Line 35: Configures the Marshaller. The true value of the JAXB_FORMATTED_OUTPUT property instructs the Marshaller to generate the XML with proper indentation.
  • Line 36: Calls the marshal() method on the Marshaller with the initialized Product object and the file to write the XML.
  • Line 37: Marshals the object to the “standard” output stream.

On running the ProductToXmlTest class, a product.xml file gets generated.

The output of the test is this.
JAXB Marshalling Test Output

Spring Framework 5
Become a Spring Framework Guru! Click here to learn more about my Spring Framework 5: Beginner to Guru online course!

Using JAXB to Unmarshall XML into Java Objects

Unmarshalling XML with JAXB to a Java object involves creating an Unmarshaller on the JAXBContext and calling the unmarshal() method. This method accepts the XML file to unmarshall.

The JUnit test class to unmarshall the generated product.xml back to the Product and User objects is this:

In this XmlToProductTest class, a JAXBContext initialized with Product is used. The createUnmarsheller() method returns an Unmarshaller. A JAXB Unmarshaller governs the process of unmarhshalling XML data into a Java object tree. Finally, the unmarshal() method unmarshals the File object for product.xml into the Product POJO.

The output on running the test is this.
JAXB Unmarshalling Test Output

Handling Collections

Often you will need to marshall Java collection objects, such as List, Set, and Map to XML, and also unmarshall XML back to collection objects.

Consider our current application where we now need to work with a list of products. To model the new requirement, let us create a Products class.

The code of the Products class is this.

In this Products class, the @XmlRootElement annotation specifies the root element of the XML as products. This class has a single List property with getter and setter methods. The add() method of this class accepts a Product object and adds it to the List.

The test class to convert a list of products to XML, is this.

The output on running the test is this.


In this post, I have covered introductory concepts on JAXB. I have previously written a post on how to use an XML Schema and JAXB to generate Java classes for a RESTful Web services here.

JAXB also comes with a JAXB Binding compiler tool, named schemagen. You can use schemagen to generate an XSD schema from Java classes.

The source code for this post is available on github. You can download it here.


About jt

    You May Also Like

    4 comments on “Using JAXB for XML with Java

    1. April 30, 2018 at 2:50 pm

      Great post! Really like the job you did. Really appreciated!!!

    2. April 30, 2018 at 3:06 pm


      Good post, thanks.

      Just a mention, from java 9 it seems JAXB is no longer included on the JVM classpath by default.

      Apparently for Java 9 at least, you can get it back by way of the “–add-modules” command line argument to the JVM.

      From Java11 (probably) more aggressive solutions will have to be employed (such as explicitly adding an additional dependency to a JAXB provider library).


    3. May 1, 2018 at 2:35 pm

      Really Nice post! Thank you John!

    4. November 9, 2018 at 5:30 pm

      I get com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions


    Leave a Reply