Convert JSON to XML using Java Jackson Library


Post Banner

In order to convert a JSON String into XML we will need to make use of the two Jackson Libraries,

Gradle:
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.4'
Maven:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.4</version>
</dependency>

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

We will first convert the JSON String to Java Object using ObjectMapper, then to XML String using XmlMapper class,

Employee POJO:
public class Employee {

    String empName;
    int empAge;
    String empDept;

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getEmpAge() {
        return empAge;
    }

    public void setEmpAge(int empAge) {
        this.empAge = empAge;
    }

    public String getEmpDept() {
        return empDept;
    }

    public void setEmpDept(String empDept) {
        this.empDept = empDept;
    }
}
JavaJsonToXML.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.IOException;

public class JavaJsonToXML {

    public static void main(String[] args) {
        try {

            //Step 1: JSON String
            String jsonString = "{\"empName\":\"Andy\",\"empAge\":22,\"empDept\":\"Insurance\"}";
            ObjectMapper objectMapper = new ObjectMapper();

            //Step 2: JSON String to Java Object
            Employee employee = objectMapper.readValue(jsonString, Employee.class);

            XmlMapper xmlMapper = new XmlMapper();

            //3. JavaObject to XML String
            String xmlString = xmlMapper.writeValueAsString(employee);
            System.out.println(xmlString);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Output:

<Employee><empName>Andy</empName><empAge>22</empAge><empDept>Insurance</empDept></Employee>



Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.