Jackson Annotations for XML – JacksonXmlRootElement

The Jackson library is widely used for working with JSON in Java. However, it also provides robust support for XML serialization and deserialization through the jackson-dataformat-xml module. One of the most useful annotations for XML handling is @JacksonXmlRootElement, which helps define the root element of an XML document.

In this blog post, we’ll explore the @JacksonXmlRootElement annotation, its usage, and how it simplifies working with XML data in Java applications.


What is @JacksonXmlRootElement?

@JacksonXmlRootElement is an annotation used to define the root element name and namespace for XML serialization. It is part of the Jackson XML module and works similarly to the @XmlRootElement annotation in JAXB.

Key Attributes:

  • localName: Specifies the name of the root XML element. If not specified, the class name is used as the default.
  • namespace: Defines the XML namespace for the root element. This is optional and defaults to an empty string.

Dependency Setup

Before using @JacksonXmlRootElement, include the Jackson XML module in your project. Add the following Maven dependency:

<dependency>

    <groupId>com.fasterxml.jackson.dataformat</groupId>

    <artifactId>jackson-dataformat-xml</artifactId>

    <version>2.15.0</version> <!-- Replace with the latest version -->

</dependency>

Basic Example

Here’s a simple example demonstrating the use of @JacksonXmlRootElement.

Java Class

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "Employee", namespace = "https://example.com/employee")
public class Employee {
    private int id;
    private String name;
    private String department;

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}

Serialization to XML

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("John Doe");
        employee.setDepartment("IT");

        XmlMapper xmlMapper = new XmlMapper();
        String xmlOutput = xmlMapper.writeValueAsString(employee);

        System.out.println("Serialized XML:\n" + xmlOutput);
    }
}

Output:

<Employee xmlns="https://example.com/employee">
    <id>1</id>
    <name>John Doe</name>
    <department>IT</department>
</Employee>

Using Default Root Element

If @JacksonXmlRootElement is omitted, the class name is used as the default root element name, and no namespace is included.

Example Without Annotation:

<Employee>

    <id>1</id>

    <name>John Doe</name>

    <department>IT</department>

</Employee>

Handling Namespaces

Namespaces in XML are crucial for distinguishing elements in complex documents. With the namespace attribute in @JacksonXmlRootElement, you can easily add a namespace to your root element.

Example with Namespace:

<Employee xmlns="https://example.com/employee">

    <id>1</id>

    <name>Jane Smith</name>

    <department>HR</department>

</Employee>

Deserialization Example

You can also deserialize XML back into Java objects using XmlMapper.

XML Input:

<Employee xmlns="https://example.com/employee">
    <id>2</id>
    <name>Jane Smith</name>
    <department>HR</department>
</Employee>

Code for Deserialization:

public class Main {
    public static void main(String[] args) throws Exception {
        String xmlInput = """
            <Employee xmlns="https://example.com/employee">
                <id>2</id>
                <name>Jane Smith</name>
                <department>HR</department>
            </Employee>
            """;

        XmlMapper xmlMapper = new XmlMapper();
        Employee employee = xmlMapper.readValue(xmlInput, Employee.class);

        System.out.println("Deserialized Object: " + employee.getName() + " (" + employee.getDepartment() + ")");
    }
}

Advantages of Using @JacksonXmlRootElement

  1. Customizable Root Names: Define meaningful and readable root element names for your XML documents.
  2. Namespace Support: Add namespaces to ensure XML compatibility and avoid element conflicts.
  3. Seamless Integration: Works seamlessly with the Jackson ecosystem, combining JSON and XML processing in the same framework.

Followers