By subclassing Unmarshaller.Listener abstract class, we can create listener class which will listen to unmarshling events. We can add our code if we want to perform some actions before and after unmarshalling. The Unmarshaller.Listener class has two methods which can be overridden. By default the methods doesn’t perform any actions.
1) public void beforeUnmarshal(Object target, Object parent)
2) public void afterUnmarshal(Object target, Object parent)
Where target represents the object being unmarshalled and parent represents the object which is holding the object being unmarshalled. The value of the parent will be null if the target variable refer to the root element of the xml.
In the case of below example instance of Country class is a root object.
We set the listener class to the marshaller by calling the below methods
unmarshaller.setListener(new UnMarshalListener());
Main class
package JAXB;
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBMarshalDemo4 {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setListener(new UnMarshalListener());
File file = new File("result.xml");
Country country = (Country)unmarshaller.unmarshal(file);
System.out.println(country);
}
}
result.xml
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<country>
<code>
<part1>1234</part1>
<part2>5678</part2>
</code>
<name>India</name>
<population>10000000</population>
</country>
Country
package JAXB;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Country {
private ZipCode code;
private String name;
private int population;
public ZipCode getCode() {
return code;
}
public void setCode(ZipCode code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append("code:").append(this.code).append(",");
sb.append("name:").append(this.name).append(",");
sb.append("population:").append(this.population).append("}");
return sb.toString();
}
}
ZipCode
package JAXB;
public class ZipCode {
private int part1;
private int part2;
public int getPart1() {
return part1;
}
public void setPart1(int part1) {
this.part1 = part1;
}
public int getPart2() {
return part2;
}
public void setPart2(int part2) {
this.part2 = part2;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append("part1:").append(this.part1).append(",");
sb.append("part2:").append(this.part2).append("}");
return sb.toString();
}
}
Listener
package JAXB;
import javax.xml.bind.Unmarshaller;;
public class UnMarshalListener extends Unmarshaller.Listener {
@Override
public void beforeUnmarshal(Object target, Object parent) {
super.beforeUnmarshal(target, parent);
System.out.println("Before unmarshal target: " + target.getClass().getName());
if(parent != null) {
System.out.println("Before unmarshal parent: " + parent.getClass().getName());
} else {
System.out.println("Before unmarshal parent: null");
}
}
@Override
public void afterUnmarshal(Object target, Object parent) {
super.afterUnmarshal(target, parent);
System.out.println("After unmarshal target: " + target.getClass().getName());
if(parent != null) {
System.out.println("After unmarshal parent: " + parent.getClass().getName());
} else {
System.out.println("After unmarshal parent: null");
}
}
}
Output
Before unmarshal target: JAXB.Country
Before unmarshal parent: null
Before unmarshal target: JAXB.ZipCode
Before unmarshal parent: JAXB.Country
After unmarshal target: JAXB.ZipCode
After unmarshal parent: JAXB.Country
After unmarshal target: JAXB.Country
After unmarshal parent: null
{code:{part1:1234,part2:5678},name:US,population:10000000}