
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert POJO to XML using the Jackson library in Java?
Jackson is a library that allows you to convert Java objects into XML and vice versa. In this example, we will demonstrate how to convert a POJO (Plain Old Java Object) into XML using the Jackson library.
Well, if you are not familiar with POJO, it is a simple Java object that does not follow any specific framework or design pattern. It is just a regular Java class with fields and methods.
We will use the method writeValueAsString()
of the XmlMapper
class to convert a POJO into XML. The XmlMapper
class is part of the Jackson library and it is used for XML serialization and deserialization.
For example, consider the following POJO:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
To convert this POJO into XML using Jackson, you need to add the Jackson library to your project. You can do this by adding the following dependency to your pom.xml
file if you are using Maven:
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.3</version> </dependency>
If you are not using Maven, you can download the Jackson library from here.
Steps to Convert POJO to XML using Jackson in Java
Following are the steps to convert a POJO to XML using Jackson in Java:
- Define your POJO class (e.g., Person) with fields and getters.
- Import the necessary Jackson classes.
- Create an instance of the POJO and XmlMapper.
- Use the
writeValueAsString()
method of theXmlMapper
class to convert the POJO to XML.
Example
In the below example, we will create a class Person with fields for name and age. We will then convert this POJO into XML using Jackson.
Following is the code to convert POJO to XML using Jackson in Java:
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class PojotoXmlEx{ public static void main(String[] args) throws IOException { // Create an instance of the Person class Person person = new Person("Ansh", 23); // Create an instance of XmlMapper XmlMapper xmlMapper = new XmlMapper(); // Convert the Person object to XML String xml = xmlMapper.writeValueAsString(person); System.out.println(xml); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Output
<?xml version="1.0" encoding="UTF-8"?> <person> <name>Ansh</name> <age>23</age> </person>