I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this article, we are going to look at how to serialize a Java object to XML data using Jackson 2.x and deserialize it back to a POJO.

We’ll focus on the basic operation that don’t require a lot of complexity or customization.

2. XmlMapper Object

XmlMapper is the main class from Jackson 2.x that helps us in serialization, so we shall need to create an instance of it:

XmlMapper mapper = new XmlMapper();

XmlMapper is available in jackson-dataformat-xml jar, so we have to add it as a dependency in our pom.xml. In this tutorial we use the version 2.7.4, but you should always check for the latest version:

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

3. Serialize Java to XML

XmlMapper is a subclass of ObjectMapper which is used in JSON serialization. However it adds some XML specific tweaks to the parent class.

We can now look at how to use it to do actual serialization. Let’s create the java class whose object to serialize:

class SimpleBean {
    private int x = 1;
    private int y = 2;
    //standard setters and getters
}

3.1. Serialize to XML String

We can serialize the Java object into an XML string:

@Test
public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException {
    XmlMapper xmlMapper = new XmlMapper();
    String xml = xmlMapper.writeValueAsString(new SimpleBean());
    assertNotNull(xml);
}

Resulting XML string (formatted for readability):

<SimpleBean>
    <x>1</x>
    <y>2</y>
</SimpleBean>

3.2. Serialize to XML file

We can also serialize the Java object to an XML file for later use:

@Test
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean());
    File file = new File("simple_bean.xml");
    assertNotNull(file);
}

The content of the resulting file simple_bean.xml :

<SimpleBean>
    <x>1</x>
    <y>2</y>
</SimpleBean>

4. Deserialize XML to Java

In this section, we shall look at how to obtain java objects from XML.

4.1. Deserialize From XML String

As with serialization, we can also deserialize an XML String back to a Java object like so:

@Test
public void whenJavaGotFromXmlStr_thenCorrect() throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    SimpleBean value = 
      xmlMapper.readValue("<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class);
    assertTrue(value.getX() == 1 && value.getY() == 2);
}

4.2. Deserialize From XML File

Likewise, if we have an XML file, this section shows how to convert it back to a Java object.

Here we first read the file into an input stream and then convert the input stream to a string with a simple utility method.

The rest of the code is similar to that in section 4.1 above:

@Test
public void whenJavaGotFromXmlFile_thenCorrect() throws IOException {
    File file = new File("simple_bean.xml");
    XmlMapper xmlMapper = new XmlMapper();
    String xml = inputStreamToString(new FileInputStream(file));
    SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class);
    assertTrue(value.getX() == 1 && value.getY() == 2);
}

The utility method:

public static String inputStreamToString(InputStream is) throws IOException {
    StringBuilder sb = new StringBuilder();
    String line;
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    br.close();
    return sb.toString();
}

5. Conclusion

This simple article illustrated how to serialize a simple POJO to XML and obtain a POJO from basic XML data.

The source code that accompanies this article is available on GitHub.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS