Convert POJO to XML using the Jackson library in Java?

JavaJSONObject Oriented ProgrammingProgramming

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We convert a POJO to XML format using the writeValueAsString() method of XmlMapper class and we need to pass the corresponding POJO instance as an argument to this method.


public String writeValueAsString(Object value) throws JsonProcessingException


import com.fasterxml.jackson.dataformat.xml.*;
public class POJOToXmlTest {
   public static void main(String args[]) throws Exception {
      try {
         XmlMapper xmlMapper = new XmlMapper();
         Person pojo = new Person();
         String xml = xmlMapper.writeValueAsString(pojo);
      } catch(Exception e) {
// Person class
class Person {
   private String firstName;
   private String lastName;
   private String address;
   public String getFirstName() {
      return firstName;
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   public String getLastName() {
      return lastName;
   public void setLastName(String lastName) {
      this.lastName = lastName;
   public String getAddress() {
      return address;
   public void setAddress(String address) {
      this.address = address;


<Person xmlns="">
Updated on 06-Jul-2020 09:24:08