XML Serialization and Deserialization with Jackson
In this section, we will discuss another important concept of Jackson, i.e., serialization and deserialization of XML data. By using Jackson, we can easily serialize a Java Object into XML data or deserialize it back to a Java object (POJO). In order to work with XML, we use Jackson “2.x”.
First, we will add Jackson “2.x”, dependency in our POM.xml file:
Note: Please add the latest version of Jackson-dataformat-xml dependency.
Let’s understand how we work with XML by using Jackson in detail.
XmlMapper Object
XmlMapper is one of the most important classes from Jackson “2.x”. It is used for the serialization and deserialization of the XML data. So, the first step for serializing and deserializing XML data is to create an instance of the XmlMapper class in the following way:
Serializing Java Object into XML
ObjectMapper is a parent class of XmlMapper that is used for serializing JSON data. If we want to make any changes to the parent class, add some XML specific changes into the parent class. Let’s understand how we can use it for serializing the following Java class:
College.java
Serialize POJO to XML String
We can easily convert POJO into XML string by simply creating an instance of XmlMapper class and use its writeValueAsString() method in the following way:
Let’s take an example to understand how we can use it to serialize Java Object into an XML string.
PojoToXMLExample1.java
Output:
Serialize POJO to XML File
In order to serialize Java object into XML File, we simply create an instance of the XmlMapper class and use its writeValue() method. In the writeValue() method, we pass two parameters, i.e., location of the XML file and instance of Java object in the following way:
Let’s take an example to understand how we can use it to serialize Java Object into an XML file.
PojoToXMLExample2.java
Output:
Deserializing XML into POJO
Just like serializing, we can easily deserialize XML data into POJO by using XmlMapper class. Let’s understand how we can deserialize the data from the XML String or an XML file one by one.
We deserialize the following XML into POJO by using XmlMapper:
De-serialize data from the XML String
In order to deserialize the XML string into POJO, follow the steps given below:
1. Create an instance of the XmlMapper class in the following way:
2. Use the readValue() method of the XmlMapper class and pass the XML string and name of the class in which XML string is to be deserialized.
Let’s take an example to understand how we can use it to deserialize the XML string into POJO.
XMLToPojoExample1.java
Output:
De-serialize data from the XML file
In order to deserialize the data of the XML file into POJO, we use the following steps:
1. Create an instance of the XmlMapper class in the following way:
2. Read data from xml file as a string and store it in a String variable.
3. Use the readValue() method of the XmlMapper class and pass the XML string and name of the class in which XML string is to be deserialized.
Let’s take an example to understand how we can use it to deserialize the data of XML file into POJO.
XMLToPojoExample2.java
Output:
Handle Capitalized Elements
In this section, we will understand how we can handle the cases where we have XML with capitalized elements to deserialize, or we need to serialize Java objects to XML with one or more capitalized elements.
Let’s understand both the cases one by one with an example.
De-serialize data from the XML string
We de-serialize the following XML into POJO by using XmlMapper:
HandleCapitalizedElement1.java
Output:
Serialize POJO to the XML string
We serialize the POJO into the XML string or XML file with one or more capitalized elements in the following way:
HandleCapitalizedElement2.java
Output:
Serialize/de-serialize List to/from XML
We can easily serialize an entire Java bean into a document by using XmlMapper class. We will create a Company.java class having fields such as comName, comEmail, address and contacts and serialize it into XML. We use the @JacksonXmlElementWrapper annotation for serializing the list.
SerializeListToXML.java
Output:
XMLMapper class automatically deserialize the list xml element without using any additional annotation. Let’s take an example to understand how we use XMLMapper to deserialize XML string having list elements.
DeserializeListFromXML.java
Output: