Skip to main content

XML Serializers

The XmlSerializer class is used to serialize POJOs into XML.

The XmlDocSerializer class is the same but serializes a <?xml?> header at the top of the file.

The class hierarchy for the builder of this serializer is:

Context.BuilderMarshallingContextable.BuilderMarshallingTraverseContext.BuilderSerializer.BuilderWriterSerializer.BuilderXmlSerializer.Builder

Refer to the builder javadocs for configurable settings.

The following pre-configured serializers are provided for convenience:

XmlSerializerDEFAULT DEFAULT_JUNEAU_NAMESPACE DEFAULT_NS DEFAULT_NS_SQ DEFAULT_NS_SQ_READABLE DEFAULT_SQ DEFAULT_SQ_READABLE

Data type representations​

The following tables are the complete reference for how each POJO datatype is rendered by XmlSerializer. They mirror how the same data structures are represented in JSON. See XML Methodology for a conceptual overview of the mapping rules.

Maps​

Loose maps and beans use the element <object> for encapsulation. _type attributes are added to bean properties or map entries if the type cannot be inferred through reflection (e.g. an Object or superclass/interface value type).

Data typeJSON exampleXML
Map
{
k1: 'v1',
k2: null
}
<object>
<k1>v1</k1>
<k2 _type='null'/>
</object>
Map
{
k1: 123,
k2: 1.23,
k3: null
}
<object>
<k1>123</k1>
<k2>1.23</k2>
<k3 _type='null'/>
</object>
Map
{
k1: 'v1',
k2: 123,
k3: 1.23,
k4: true,
k5: null
}
<object>
<k1>v1</k1>
<k2 _type='number'>123</k2>
<k3 _type='number'>1.23</k3>
<k4 _type='boolean'>true</k4>
<k5 _type='null'/>
</object>

Arrays and collections​

Loose collections and arrays use the element <array> for encapsulation. Collections (e.g. List, Set) serialize identically to their array equivalents.

Data typeJSON exampleXML
String[]
['foo', null]
<array>
<string>foo</string>
<null/>
</array>
Number[]
[123, 1.23, null]
<array>
<number>123</number>
<number>1.23</number>
<null/>
</array>
Object[]
['foo', 123, 1.23, true, null]
<array>
<string>foo</string>
<number>123</number>
<number>1.23</number>
<boolean>true</boolean>
<null/>
</array>
String[][]
[['foo', null], null]
<array>
<array>
<string>foo</string>
<null/>
</array>
<null/>
</array>
int[]
[123]
<array>
<number>123</number>
</array>
boolean[]
[true]
<array>
<boolean>true</boolean>
</array>
List<String>
['foo', null]
<array>
<string>foo</string>
<null/>
</array>
List<Number>
[123, 1.23, null]
<array>
<number>123</number>
<number>1.23</number>
<null/>
</array>
List<Object>
['foo', 123, 1.23, true, null]
<array>
<string>foo</string>
<number>123</number>
<number>1.23</number>
<boolean>true</boolean>
<null/>
</array>

Beans​

Data typeJSON exampleXML
class MyBean {
public String a;
public int b;
public Object c; // String value
public Object d; // Integer value
public MyBean2 e;
public String[] f;
public int[] g;
}

class MyBean2 {
String h;
}
{
a: 'foo',
b: 123,
c: 'bar',
d: 456,
e: {
h: 'baz'
},
f: ['qux'],
g: [789]
}
<object>
<a>foo</a>
<b>123</b>
<c>bar</c>
<d _type='number'>456</d>
<e>
<h>baz</h>
</e>
<f>
<string>qux</string>
</f>
<g>
<number>789</number>
</g>
</object>

Beans with Map properties​

Data typeJSON exampleXML
class MyBean {
public Map<String,String> a;
public Map<String,Number> b;
public Map<String,Object> c;
}
{
a: { k1: 'foo' },
b: { k2: 123 },
c: {
k3: 'bar',
k4: 456,
k5: true,
k6: null
}
}
<object>
<a>
<k1>foo</k1>
</a>
<b>
<k2>123</k2>
</b>
<c>
<k3>bar</k3>
<k4 _type='number'>456</k4>
<k5 _type='boolean'>true</k5>
<k6 _type='null'/>
</c>
</object>