Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Switch branches/tags
jackson-dataformat-xml-2.9.7 jackson-dataformat-xml-2.9.6 jackson-dataformat-xml-2.9.5 jackson-dataformat-xml-2.9.4 jackson-dataformat-xml-2.9.3 jackson-dataformat-xml-2.9.2 jackson-dataformat-xml-2.9.1 jackson-dataformat-xml-2.9.0 jackson-dataformat-xml-2.9.0.pr4 jackson-dataformat-xml-2.9.0.pr3 jackson-dataformat-xml-2.9.0.pr2 jackson-dataformat-xml-2.9.0.pr1 jackson-dataformat-xml-2.8.11 jackson-dataformat-xml-2.8.10 jackson-dataformat-xml-2.8.9 jackson-dataformat-xml-2.8.8 jackson-dataformat-xml-2.8.7 jackson-dataformat-xml-2.8.6 jackson-dataformat-xml-2.8.5 jackson-dataformat-xml-2.8.4 jackson-dataformat-xml-2.8.3 jackson-dataformat-xml-2.8.2 jackson-dataformat-xml-2.8.1 jackson-dataformat-xml-2.8.0 jackson-dataformat-xml-2.8.0.rc2 jackson-dataformat-xml-2.8.0.rc1 jackson-dataformat-xml-2.7.9 jackson-dataformat-xml-2.7.8 jackson-dataformat-xml-2.7.7 jackson-dataformat-xml-2.7.6 jackson-dataformat-xml-2.7.5 jackson-dataformat-xml-2.7.4 jackson-dataformat-xml-2.7.3 jackson-dataformat-xml-2.7.2 jackson-dataformat-xml-2.7.1 jackson-dataformat-xml-2.7.0 jackson-dataformat-xml-2.6.7 jackson-dataformat-xml-2.6.6 jackson-dataformat-xml-2.6.5 jackson-dataformat-xml-2.6.4 jackson-dataformat-xml-2.6.4-1 jackson-dataformat-xml-2.6.3 jackson-dataformat-xml-2.6.2 jackson-dataformat-xml-2.6.1 jackson-dataformat-xml-2.6.0 jackson-dataformat-xml-2.5.5 jackson-dataformat-xml-2.5.4 jackson-dataformat-xml-2.5.3 jackson-dataformat-xml-2.5.2 jackson-dataformat-xml-2.5.1 jackson-dataformat-xml-2.5.0 jackson-dataformat-xml-2.4.6 jackson-dataformat-xml-2.4.5 jackson-dataformat-xml-2.4.4 jackson-dataformat-xml-2.4.3 jackson-dataformat-xml-2.4.2 jackson-dataformat-xml-2.4.1 jackson-dataformat-xml-2.4.0 jackson-dataformat-xml-2.3.5 jackson-dataformat-xml-2.3.4 jackson-dataformat-xml-2.3.3 jackson-dataformat-xml-2.3.2 jackson-dataformat-xml-2.3.1 jackson-dataformat-xml-2.3.0 jackson-dataformat-xml-2.2.4 jackson-dataformat-xml-2.2.3 jackson-dataformat-xml-2.2.2 jackson-dataformat-xml-2.2.1 jackson-dataformat-xml-2.2.0 jackson-dataformat-xml-2.1.5 jackson-dataformat-xml-2.1.4 jackson-dataformat-xml-2.1.3 jackson-dataformat-xml-2.1.2 jackson-dataformat-xml-2.1.1 jackson-dataformat-xml-2.1.0 jackson-dataformat-xml-2.0.6 jackson-dataformat-xml-2.0.5 jackson-dataformat-xml-2.0.4 jackson-dataformat-xml-2.0.3 jackson-dataformat-xml-2.0.2 jackson-dataformat-xml-2.0.1 jackson-dataformat-xml-2.0.0
Nothing to show
Clone or download
Latest commit 993878c Aug 22, 2018



This projects contains Jackson extension component for reading and writing XML encoded data.

Further, the goal is to emulate how JAXB data-binding works with "Code-first" approach (that is, no support is added for "Schema-first" approach). Support for JAXB annotations is provided by JAXB annotation module; this module provides low-level abstractions (JsonParser, JsonGenerator, JsonFactory) as well as small number of higher level overrides needed to make data-binding work.

It is worth noting, however, that the goal is NOT to be full JAXB clone; or to be general purpose XML toolkit.


  • While XML serialization should ideally be similar to JAXB output, deviations are not necessarily considered bugs -- we do "best-effort" matching
  • What should be guaranteed is that any XML written using this module must be readable using module as well: that is, we do aim for full XML serialization.
  • From above: there are XML constructs that module will not be able to handle; including some cases JAXB supports
  • This module may, however, also support constructs and use cases JAXB does not handle: specifically, rich type and object id support of Jackson are supported.

Build Status Maven Central Javadoc


As of version 2.3, module is fully functional and considered production ready.

Maven dependency

To use Jackson 2.x compatible version of this extension on Maven-based projects, use following dependency:


(or whatever version is most up-to-date at the moment)

Also: you usually also want to make sure that XML library in use is Woodstox since it is not only faster than Stax implementation JDK provides, but also works better and avoids some known issues like adding unnecessary namespace prefixes. You can do this by adding this in your pom.xml:



Constructing Mapper

Although module implements low-level (JsonFactory / JsonParser / JsonGenerator) abstractions, most usage is through data-binding level. This because a small number of work-arounds have been added at data-binding level, to work around XML peculiarities: that is, stream of JsonTokens that parser produces has idiosyncracies that need special handling.

Usually you either create XmlMapper simply by:

XmlMapper mapper = new XmlMapper();

but in case you need to configure settings, you will want to do:

JacksonXmlModule module = new JacksonXmlModule();
// and then configure, for example:
XmlMapper xmlMapper = new XmlMapper(module);
// and you can also configure AnnotationIntrospectors etc here:

as many features that XmlMapper needs are provided by JacksonXmlModule; default XmlMapper simply constructs module with default settings.

Alternatively, sometimes you may want/need to configure low-level XML processing details controlled by underlying Stax library (Woodstox, Aalto or JDK-default Oracle implementation). If so, you will need to construct XmlMapper with properly configured underlying factories. This usually looks something like:

XMLInputFactory ifactory = new WstxInputFactory(); // Woodstox XMLInputFactory impl
ifactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);
// configure
XMLOutputFactory ofactory = new WstxOutputFactory(); // Woodstox XMLOutputfactory impl
ofactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);
XmlFactory xf = new XmlFactory(ifactory, ofactory);
XmlMapper mapper = new XmlMapper(xf); // there are other overloads too

For configurable properties, you may want to check out Configuring Woodstox XML parser

Android quirks

Usage of this library on Android is currently not supported. This is due to the fact that the Stax API is unavailable on the Android platform, and attempts to declare an explicit dependency on the Stax API library will result in errors at build time (since the inclusion of the javax.* namespace in apps is restricted). For more on the issues, see:

Note that as per articles linked to it MAY be possible to use the module on Android, but it unfortunately requires various work-arounds and development team can not do much to alleviate these issues. Suggestions for improvements would be welcome; discussions on Jackson users list encouraged.

Serializing POJOs as XML

Serialization is done very similar to JSON serialization: all that needs to change is ObjectMapper instance to use:

// Important: create XmlMapper; it will use proper factories, workarounds
ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(new Simple());
// or
xmlMapper.writeValue(new File("/tmp/stuff.json"), new Simple());

and with POJO like:

public class Simple {
    public int x = 1;
    public int y = 2;

you would get something like:


(except that by default output is not indented: you can enabled indentation using standard Jackson mechanisms)

Deserializing POJOs from XML

Similar to serialization, deserialization is not very different from JSON deserialization:

ObjectMapper xmlMapper = new XmlMapper();
Simple value = xmlMapper.readValue("<Simple><x>1</x><y>2</y></Simple>", Simple.class);

Incremental/partial reading/writing (2.4+)

It is also possible to do incremental writes. This is done by creating Stax XMLInputFactory separately (similar to how with JSON you would create JsonGenerator), and then:

// First create Stax components we need
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
StringWriter out = new StringWriter();
XMLStreamWriter sw = xmlOutputFactory.createXMLStreamWriter(out);

// then Jackson components
XmlMapper mapper = new XmlMapper(xmlInputFactory);


// Write whatever content POJOs...
SomePojo value1 = ...;
OtherPojo value2 = ...;
mapper.writeValue(sw, value1);
mapper.writeValue(sw, value2);
// and/or regular Stax output
sw.writeComment("Some insightful commentary here");

Similarly it is possible to read content, sub-tree by sub-tree; assuming similar XML content we would use

XMLInputFactory f = XMLInputFactory.newFactory();
File inputFile = ...;
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(inputFile));

XmlMapper mapper = new XmlMapper();
sr.next(); // to point to <root>
sr.next(); // to point to root-element under root
SomePojo value1 = mapper.readValue(sr, SomePojo.class);
// sr now points to matching END_ELEMENT, so move forward
sr.next(); // should verify it's either closing root or new start, left as exercise
OtherPojo value = mapper.readValue(sr, OtherPojo.class);
// and more, as needed, then

Additional annotations

In addition to standard Jackson annotations and optional JAXB (javax.xml.bind.annotation), this project also adds couple of its own annotations for convenience, to support XML-specific details:

  • @JacksonXmlElementWrapper allows specifying XML element to use for wrapping List and Map properties
  • @JacksonXmlProperty allows specifying XML namespace and local name for a property; as well as whether property is to be written as an XML element or attribute.
  • @JacksonXmlRootElement allows specifying XML element to use for wrapping the root element (default uses 'simple name' of the value class)
  • @JacksonXmlText allows specifying that value of one property is to be serialized as "unwrapped" text, and not in an element.
  • @JacksonXmlCData allows specifying that the value of a property is to be serialized within a CData tag.

for longer description, check out XML module annotations.

Known Limitations

Currently, following limitations exist beyond basic Jackson (JSON) limitations:

  • Streaming model is only meant to be used through databinding: direct usage is possible but not supported
  • Tree Model is only supported in limited fashion and its use is recommended against: since tree model is based on JSON information model, it does not match XML infoset
    • Java arrays and Collections can be written, but can not be read, since it is not possible to distinguish Arrays and Objects without additional information.
    • Mixed content (both textual content and elements as children of an element) not supported: text, if any, is lost
    • Repeated elements with same name are handled so that only the last element is included, others are ignored
  • Root value should be a POJO; and specifically following types can be serialized as properties but may not work as intended as root values
    • Primitive/Wrapper values (like java.lang.Integer)
    • Enums
    • Java arrays
    • java.util.Collection values (Lists, Sets)
    • Note: over time some level of support has been added, and Collections, for example, often work.
  • Lists and arrays are "wrapped" by default, when using Jackson annotations, but unwrapped when using JAXB annotations (if supported, see below)
    • @JacksonXmlElementWrapper.useWrapping can be set to 'false' to disable wrapping
    • JacksonXmlModule.setDefaultUseWrapper() can be used to specify whether "wrapped" or "unwrapped" setting is the default


See Also