Kotlin data class to xml
Tags: kotlin, springboot, fasterxml, xml
Published: 2020-07-08 08:47:17

---
title: Kotlin data class to xml
published: true
description: How to serialize kotlin data class to xml using fasterxml
tags: kotlin, springboot, fasterxml, xml
//cover_image: https://direct_url_to_image.jpg
---

Let’s say you have this sample data class that you want to serialize to xml
```
data class SampleDataClass(
        val externalTxnId: String,
        val merchantTxnId: String,
        val reference: String
)
```
###Steps

* If you’re using maven, add the [jackson-dataformat-xml](https://github.com/FasterXML/jackson-dataformat-xml) dependency on your pom.xml file
```
<dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
   <version>2.10.1</version>
</dependency>
```
* Initialize the XmlMapper
```
val xmlMapper = XmlMapper(
        JacksonXmlModule().apply { setDefaultUseWrapper(false) }
).apply {
        enable(SerializationFeature.INDENT_OUTPUT)
}
```
* Initialize your data class and use the mapper’s writeValueAsString() method to get the xml value.
```
fun main() {
        val xmlMapper = XmlMapper(
                JacksonXmlModule().apply { setDefaultUseWrapper(false) }
        ).apply {
                enable(SerializationFeature.INDENT_OUTPUT)
                enable(SerializationFeature.WRAP_ROOT_VALUE)
        }
        
        val data = SampleDataClass(
                externalTxnId = "07026984141550752666",
                merchantTxnId = "07026984141550752666",
                reference     = "MERCHPAY"
        )
        val xml = xmlMapper.writeValueAsString(data)
        println(xml)
}

//This will give out the following output

<SampleDataClass>
  <externalTxnId>07026984141550752666</externalTxnId>
  <merchantTxnId>07026984141550752666</merchantTxnId>
  <reference>MERCHPAY</reference>
</SampleDataClass>
```
Default root element used is the simple name of the data class. So if you don’t want to use the defaults, that’s where the annotation @JacksonXmlRootElement for root element and @JacksonXmlProperty for fields come in.

Let’s say we want the root element to be labelled COMMAND and the fields externalTxnId to EXTTRID, merchantTxnId to MERCHANT_TXN_ID and finally reference to REFERENCE. To have this implemented we’ll add the annotations to the data class as below:

```
@JacksonXmlRootElement(localName = "COMMAND")
data class SampleDataClass(
        @field:JacksonXmlProperty(localName = "EXTTRID")
        val externalTxnId: String,
        @field:JacksonXmlProperty(localName = "MERCHANT_TXN_ID")
        val merchantTxnId: String,
        @field:JacksonXmlProperty(localName = "REFERENCE")
        val reference: String
)fun main() {
        val xmlMapper = XmlMapper(
                JacksonXmlModule().apply { setDefaultUseWrapper(false) }
        ).apply {
                enable(SerializationFeature.INDENT_OUTPUT)
                enable(SerializationFeature.WRAP_ROOT_VALUE)
        }
        
        val data = SampleDataClass(
                externalTxnId = "07026984141550752666",
                merchantTxnId = "07026984141550752666",
                reference     = "MERCHPAY"
        )
        val xml = xmlMapper.writeValueAsString(data)
        println(xml)
}//This will give the following output<COMMAND>
  <EXTTRID>07026984141550752666</EXTTRID>
  <MERCHANT_TXN_ID>07026984141550752666</MERCHANT_TXN_ID>
  <REFERENCE>MERCHPAY</REFERENCE>
</COMMAND>
```