Serialization is the process of converting a Java object into a format that can be easily stored or transmitted. While JSON is commonly used, XML is still a popular data interchange format in many domains, such as web services and enterprise applications. In this blog post, we’ll explore how to convert Java objects to XML using the Jackson API.
Jackson is a versatile library mainly known for JSON processing, but it also provides powerful features for working with XML when integrated with the jackson-dataformat-xml
module.
Why Use Jackson for XML Serialization?
- Ease of Use: Jackson simplifies the process with annotations to customize XML output.
- Integration: Jackson works seamlessly with Java, making it a popular choice for developers.
- Flexibility: You can handle both JSON and XML with the same library, reducing dependencies.
Prerequisites
Before we start coding, ensure you have the following:
- Java 8+
- Maven or any build tool
- Jackson Core and Jackson Dataformat XML dependencies in your project.
Maven Dependency
Add the following dependencies to your pom.xml
:
<dependencies>
<!-- Jackson Core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<!-- Jackson XML -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>
Step-by-Step Guide to Serialize Java Objects to XML
1. Create a Sample Java Class
Let’s create a simple Book
class with some fields and annotations to customize the XML output.
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class Book {
@JacksonXmlProperty(localName = "Title")
private String title;
@JacksonXmlProperty(localName = "Author")
private String author;
@JacksonXmlProperty(localName = "Price")
private double price;
// Constructors, Getters, and Setters
public Book() {}
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// Getters and Setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
2. Configure the XML Mapper
The XmlMapper
class in Jackson provides functionality for XML serialization.
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XMLSerializationExample {
public static void main(String[] args) {
try {
// Create a Book object
Book book = new Book("Effective Java", "Joshua Bloch", 45.0);
// Initialize XmlMapper
XmlMapper xmlMapper = new XmlMapper();
// Serialize Java Object to XML
String xmlOutput = xmlMapper.writeValueAsString(book);
// Print the XML Output
System.out.println(xmlOutput);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Output
When you run the above program, it produces the following XML:
<Book>
<Title>Effective Java</Title>
<Author>Joshua Bloch</Author>
<Price>45.0</Price>
</Book>
Customizing XML Structure
The Jackson API allows extensive customization of the XML structure using annotations:
Root Element Name
To change the root element’s name, use the @JacksonXmlRootElement
annotation.
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "LibraryBook")
public class Book {
// Fields and annotations remain the same
}
<LibraryBook>
instead of <Book>
.