Over a million developers have joined DZone.

Parse XML to Java Objects Using Jackson

DZone's Guide to

Parse XML to Java Objects Using Jackson

Check out this short tutorial on how to parse XML to Java objects using Jackson.

· Web Dev Zone ·
Free Resource

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

Parsing the XML document to Java objects using Jackson library is quite simple. 

The following is the XML that we are going to parse.

    <employee id="EMP0001">
    <employee id="EMP0002">

To parse the above XML, we will use Jackson library. We have also included Apache Commons library for converting bytes to String. The maven dependency is as follows.


We need to create the model objects according to the XML. There will be two model objects.


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

import java.util.Arrays;

@JacksonXmlRootElement(localName = "employees") public final class Employees {
    @JacksonXmlElementWrapper(localName = "employee", useWrapping = false)
    private Employee[] employee;

    public Employees() {

    public Employees(Employee[] employee) {
        this.employee = employee;

    public Employee[] getEmployee() {
        return employee;

    public void setEmployee(Employee[] employee) {
        this.employee = employee;

    @Override public String toString() {
        return "Employees{" +
                "employees=" + Arrays.toString(employee) +


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

public final class Employee {
    @JacksonXmlProperty(localName = "id", isAttribute = true)
    private String id;
    @JacksonXmlProperty(localName = "first_name")
    private String firstName;
    @JacksonXmlProperty(localName = "last_name")
    private String lastName;
    @JacksonXmlProperty(localName = "age")
    private int age;

    public Employee() {

    public Employee(String id, String firstName, String lastName, int age) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;

    public String getId() {
        return id;

    public void setId(String id) {
        this.id = id;

    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 int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    @Override public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", age=" + age +

There are few annotations that were added to the model objects which play a key role. They are JacksonXmlRootElement, JacksonXmlElementWrapper and JacksonXmlProperty.

The following is the code to parse the above XML document.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Parser {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new XmlMapper();
        // Reads from XML and converts to POJO
        Employees employees = objectMapper.readValue(
                StringUtils.toEncodedString(Files.readAllBytes(Paths.get("/tmp/employees.xml")), StandardCharsets.UTF_8),
        // Reads from POJO and converts to XML
        objectMapper.writeValue(new File("/tmp/fieldsets.xml"), fieldSet);

#1 for location developers in quality, price and choice, switch to HERE.

jackson ,xml ,java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}