Software Development

JAXB – Perform Pre- and Post-processing with Unmarshaller.Listener


This post demonstrates how to perform pre- and post-processing with Unmarshaller.Listener. For instance you may want to compare the contents of an object before and after you unmarshal an XML. Another use-case is when you may need to store unmarshalled values to some list not mapped to any XML element. This is achieved by creating a class that extends Unmarshaller.Listener and overriding two (2) methods – beforeUnmarshal and afterUnmarshal.

Software Requirements

  • Windows 10
  • Java 8 (Source Level = 1.8)
  • Eclipse Mars.2 Release (4.5.2)


Unmarshaller.Listener is an abstract class available in java.xml.bind package. It has two (2) methods.


Person Unmarshal Listener

PersonUnmarshallListener is our listener implementation that prints out some texts, and store the name value to a list. For instance, afterUnmarshal prints out "AFTER UNMARSHAL", the contents of the object, and store the name of a person in a list. For beforeUnmarshal, it prints out "BEFORE UNMARSHAL" and the contents of the initial object.


Java Model and Test XML

Our Java class that represents the contents of our test XML. We have a List field annotated with @XmlTransient. This field is not mapped to any element in the test XML and is used to store the name of the Person object.

Below is our test XML.

The Main class

First we need to instantiate our listener and set it as the unmarshaller‘s listener.


Sample Output

Download the codes

You may download the codes from


You Might Also Like