# XML Serialization [TOC] ###### tags: `java` `xml` --- Jackson Project<SUP>[๐Ÿ”—](https://github.com/FasterXML/jackson)</SUP> > โ€ฆsupports XML; provides both streaming and databind implementations. Similar to JAXB' "code-first" mode (no support for "XML Schema first", but can use JAXB beans)โ€ฆ ## Maven Repositories<SUP>[๐Ÿ”—](https://mvnrepository.com/artifact/com.fasterxml.jackson.core)</SUP> ```xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.5</version> </dependency> ``` :::info ๅ…ถๅฎƒ Jackson-related repositories [ใ„‰ใ„งห‡ใ„ใ„งใ„šห‰ใ„Œใ„šห™](https://mvnrepository.com/artifact/com.fasterxml.jackson)ใ€‚ ::: ## Configuring the Jackson XML Module<SUP>[๐Ÿ”—](https://stackify.com/java-xml-jackson/)</SUP> > The absolute simplest way of working with this is to just use the default configuration: > >```java >ObjectMapper xmlMapper = new XmlMapper(); >``` > > To construct the Jackson Module manually: > >```java >JacksonXmlModule jacksonXmlModule = new JacksonXmlModule(); >jacksonXmlModule.setDefaultUseWrapper(false); >ObjectMapper xmlMapper = new XmlMapper(jacksonXmlModule); >``` > > As of version **2.9.0**, the only configuration options for the XML Module are: > >- `setDefaultUseWrapper` โ€“ defines whether or not to use a wrapper for non-annotated List properties by default >- `setXMLTextElementName` โ€“ defines the virtual name to use when processing character data sections โ€“ when not binding to Java beans > > To configure it to produce indented output: > >```java >xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); >``` ## Jackson Annotations for Serialization > Given the following bean: > >```java >@JsonPropertyOrder({"age", "id", "name"}) >public class Person { > > @JsonProperty("_id") > private String id; > > private String name; > > private int age; > > @JsonIgnore > private String note; >} >``` > > will produce this XML: > > >```xml ><Person> > <age>4</age> > <_id>12345</_id> > <name>George</name> ></Person> >``` ## Additional Jackson XML-specific annotations<SUP>[๐Ÿ”—](https://github.com/FasterXML/jackson-dataformat-xml/wiki/Jackson-XML-annotations)</SUP> ### `@JacksonXmlRootElement` > Allows specifying XML element to use for wrapping the root element (default uses *simple name* of the value class). This only needs to be used for objects serialized as root values: for other values property name is used; for root values there is no referring property and separate annotation is needed on class itself. > > Properties: > > `namespace` (default: `""`): Which XML namespace to use for root element > `localName`: Local name of root element > > Letโ€™s look at this POJO: > >```java >@JacksonXmlRootElement( > namespace = "urn:somenamespace", > localName = "PersonData" >) >public class Person { > > private String id; > > private String name; > > private String note; >} >``` > > When serialized, this will result in the following XML: >```java ><PersonData xmlns="urn:somenamespace"> > <id xmlns="">12345</id> > <name xmlns="">Graham</name> > <note xmlns="">Hello</note> ></PersonData> >``` ### `@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. > > Properties: > > - `namespace` (default: `""`): Which XML namespace to use for property element or attribute > - `localName`: Local name of property element or attribute > - `isAttribute` (default: `false`): Whether property should be serialized as an attribute (`true`) or element (`false`) > > The following bean: > >```java >public class Person { > > @JacksonXmlProperty( > isAttribute = true, > namespace = "urn:somenamespace", > localName = "_id" > ) > private String id; > > @JacksonXmlProperty(namespace = "urn:anothernamespace") > private String name; > > private String note; >} >``` > > generates the following XML output: > >```xml ><Person xmlns:wstxns1="urn:somenamespace" wstxns1:_id="12345"> > <wstxns2:name xmlns:wstxns2="urn:anothernamespace">Graham</wstxns2:name> > <note>Hello</note> ></Person> >``` ### `@JacksonXmlElementWrapper` > Allows specifying XML element to use for wrapping List and Map properties; or disabling use (with `useWrapping` set to `false`). > > Properties: > > - `namespace` (default: `""`): Which XML namespace to use for wrapper element (defaults to: `""`) > - `localName`: Local name for wrapper element > - `useWrapping` (default: `true`) Whether to use wrapper element or not: by specifying `false` one can disable use ### `@JacksonXmlText` > Allows specifying that value of one property is to be serialized as **unwrapped** text, and not in an element. > > Properties: > > `value` (default: `true`): Whether annotated property value is to be serialized as basic XML text (without surrounding XML element) or not. ## References - [Solving the XML Problem with Jackson](https://stackify.com/java-xml-jackson/)