Mapping xml to Java POJOs with Jackson

Jackson is typically used for Json serialization and deserialization, but with the jackson-dataformat-xml dependency you can also use Jackson to easily map to/from XML too.

Maven dependencies:

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

If you have additional elements and attributes on elements that you’re not interested in, similar to Json, just add the ignore annotation at class level:

@JsonIgnoreProperties(ignoreUnknown = true)

In some XML docs you can have elements with the same name at the same level without a parent to exclusively group those elements. For example you can have xml that looks like this with a grouping:

<example>
    <name>example1</name>
    <items>
        <item>1</item>
        <item>2</item>
    </items>
</example>

This would be automatically handled by Jackson if your POJO class has a list of Item called items:

private List<Item> items;

but sometimes you’ll have a doc that looks like this:

<example>
    <name>example1</name>
    <item>1</item>
    <item>2</item>
</example>

To map this you need to use this annotation:

@JacksonXmlElementWrapper(useWrapping = false)
private List<Item> item;

Debugging JAXB unmarshalling issues

JAXB appears to fail silently in some cases if the XML it’s attempting to unmarshall to mapped classes doesn’t have the necessary mapped properties.

You can get additional output by adding the following:

-Djaxb.debug=true

– displays information during JAXB initialization

Before you call unmarshall() on your Unmarshaller, call setEventHandler() and add a DefaultValidationEventHandler as follows:

Unmarshaller um = jaxbContext.createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

– this will output additional failure information about missing mappings for xml elements, useful if your mapped class ends up with missing values.

Things you always forget: XML Character Entities

Certain characters break XML or need to be escaped in HTML so they are rendered literally and not interpreted themselves as markup. There’s a few predefined character entities that are commonly used:

  • &lt;      :     <
  • &gt;     :     >
  • &amp; :    &

For other characters though, you sometimes need to use their Unicode encoded value. For example to literally display { and } in a JSP or JSF page (since they are used in EL syntax ${} and #{} ), you can use their unicode values like this:

  • &#x007b;     :     {
  • &#x007d;     :     }

There’s many unicode ref charts online, here’s one that I’ve used:

http://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF