Jackson XML Serialization and Deserialization in Java

The Jackson data format XML module provides a fast and familiar way to process XML data in Java by extending Jackson’s core data-binding capabilities. By replacing the standard JSON ObjectMapper with XmlMapper, developers can seamlessly serialize Java objects into XML strings or documents and deserialize incoming XML payloads back into strongly typed Java instances with minimal configuration.

Required Dependency

To use Jackson for XML processing, include the jackson-dataformat-xml dependency in your project.

For Maven:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.17.0</version>
</dependency>

For Gradle:

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.0'

Core Component: XmlMapper

The foundation of Jackson’s XML support is the XmlMapper class. It extends Jackson’s core ObjectMapper and provides dedicated methods for parsing and generating XML trees.

XmlMapper xmlMapper = new XmlMapper();

Serializing Java Objects to XML

Serialization converts a Java Plain Old Java Object (POJO) into an XML representation using writeValueAsString() or writeValue().

Example POJO

public class User {
    private String id;
    private String name;
    private String email;

    public User() {}

    public User(String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    // Standard getters and setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

Serialization Code

XmlMapper xmlMapper = new XmlMapper();
User user = new User("101", "John Doe", "john.doe@example.com");

String xmlOutput = xmlMapper.writeValueAsString(user);
System.out.println(xmlOutput);

Output

<User>
    <id>101</id>
    <name>John Doe</name>
    <email>john.doe@example.com</email>
</User>

Deserializing XML to Java Objects

Deserialization reads XML content from a String, File, InputStream, or Reader and maps the values to the corresponding fields in a POJO using readValue().

Deserialization Code

String xmlInput = "<User><id>101</id><name>John Doe</name><email>john.doe@example.com</email></User>";

XmlMapper xmlMapper = new XmlMapper();
User user = xmlMapper.readValue(xmlInput, User.class);

System.out.println("User Name: " + user.getName());

Customizing XML Structure with Annotations

Standard POJO serialization creates XML elements using default Java field names. Jackson provides specific annotations to customize the XML structure, element names, attributes, and wrapper elements.

Key Annotations

Example with Annotations

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.List;

@JacksonXmlRootElement(localName = "employee_record")
public class Employee {

    @JacksonXmlProperty(isAttribute = true)
    private String id;

    @JacksonXmlProperty(localName = "full_name")
    private String name;

    @JacksonXmlElementWrapper(localName = "skills_list")
    @JacksonXmlProperty(localName = "skill")
    private List<String> skills;

    // Getters and Setters
}

Resulting XML Output

<employee_record id="E-554">
    <full_name>Jane Smith</full_name>
    <skills_list>
        <skill>Java</skill>
        <skill>Spring</skill>
    </skills_list>
</employee_record>

Handling Unknown Properties and Pretty Printing

To prevent deserialization failures when unexpected XML elements are encountered, disable FAIL_ON_UNKNOWN_PROPERTIES:

xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

To format the serialized XML with indentation for better readability:

String prettyXml = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);