repost: XML Serialization and Deserialization with Jackson
# 1. Overview
In this tutorial, we’ll learn how to serialize Java objects to XML data using Jackson 2.x, and deserialize them back to a POJO.
We’ll focus on the basic operation that doesn’t require a lot of complexity or customization.
# 2. XmlMapper Object
XmlMapper is the main class from Jackson 2.x that helps us in serialization, so we’ll need to create an instance of it:
1 | XmlMapper mapper = new XmlMapper(); |
This mapper is available in the jackson-dataformat-xml jar, so we have to add it as a dependency to our pom.xml:
1 | <dependency> |
Please check the latest version of the jackson-dataformat-xml dependency in the Maven repository.
# 3. Serialize Java to XML
XmlMapper is a subclass of ObjectMapper, which is used in JSON serialization; however, it adds some XML specific tweaks to the parent class.
Let’s look at how to use it to do the actual serialization. Let’s create a Java class first:
1 | class SimpleBean { |
# 3.1. Serialize to the XML String
We can serialize our Java object into the XML String:
1 |
|
As a result, we’ll get:
1 | <SimpleBean> |
# 3.2. Serialize to the XML File
We can also serialize our Java object to the XML file:
1 |
|
And below we can see the content of the resulting file named simple_bean.xml:
1 | <SimpleBean> |
# 4. Deserialize XML to Java
In this section, we’ll look at how to obtain Java objects from XML.
# 4.1. Deserialize From the XML String
As with serialization, we can also deserialize an XML String back to a Java object:
1 |
|
# 4.2. Deserialize From the XML File
Likewise, if we have an XML file, we can convert it back to a Java object.
1 |
|
# 5. Handling Capitalised Elements
In this section, we’ll discuss how to handle scenarios where we either have XML with capitalized elements to deserialize, or we need to serialize Java objects to XML with one or more elements capitalized.
# 5.1. Deserialize From the XML String
Let’s say we have an XML with one field capitalized:
1 | <SimpleBeanForCapitalizedFields> |
In order to correctly handle capitalized elements, we need to annotate the “x” field with the @JsonProperty annotation:
1 | class SimpleBeanForCapitalizedFields { |
We can now correctly deserialize an XML String back to a Java object:
1 |
|
# 5.2. Serialize to the XML String
By annotating the required fields with @JsonProperty, we can correctly serialize a Java object into an XML String with one or more capitalized elements:
1 |
|
# 6. Serialize List to XML
The XmlMapper is able to serialize an entire Java bean into a document. To convert a Java object to XML, we’ll take a simple example with a nested object and arrays.
Our intent is to serialize a Person object, along with its composed Address object, into XML.
Our final XML will look something like:
1 | <Person> |
Notice that our phone numbers are encapsulated in a phoneNumbers wrapper, while our address isn’t.
We can express this nuance via the @JacksonXMLElementWrapper annotation in our Person class:
1 | public final class Person { |
In fact, we can change the wrapping element name with @JacksonXmlElementWrapper(localName = ‘phoneNumbers’). Or, if we don’t want to wrap our elements, we can disable the mapping with @JacksonXmlElementWrapper(useWrapping = false).
Then we’ll define our Address type:
1 | public class Address { |
Jackson takes care of the rest for us. Like before, we can simply call writeValue again:
1 | private static final String XML = "<Person>...</Person>"; |
# 7. Deserialize XML to List
Jackson can read XML that contains lists of objects as well.
If we take our same XML as before, the readValue method does just fine:
1 |
|
# 8. Conclusion
This brief article illustrated how to serialize a simple POJO to XML, and obtain a POJO from basic XML data.
We also explored how to serialize and deserialize complex beans that contain collections.