DEV Community

Gordon Passy
Gordon Passy

Posted on

Kotlin data class to xml

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

<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>

Discussion (3)

Collapse
davidbojkovski profile image
David Bojkovski

Why would you want to serialize a data class to XML? Could you give a real example of where this might happen?

Collapse
gordonpassy profile image
Gordon Passy Author

Hi David. An example would be some payment APIs such as Temenos T24

Collapse
muyembeii profile image
MuyembeII

Consume a SOAP web service end point