How to generate XML with Kotlin Extension functions and Lambdas in Android app — Kotlin DSL

Mohammed Audhil
AndroidPub
Published in
1 min readJan 24, 2018

Working with JSON is like a piece of cake to any mobile developer. Unfortunately, sometimes we may need to work with XML as well to support age old webservice APIs. There are several ways to generate XML in android, but none looks fancy to me, all seems verbose, error prone, and overkill. While searching for a way to fix this, I reached here. The author had used Kotlin Extension functions and Lambdas very well to avoid verbose in XML generation. Being a Kotlin fan and after trying a while, I made few extension functions to generate XML that makes life hell of a lot easier.

Let us consider the required XML as below,

One of the ugliest, toughest, verbose, error prone way…

Easiest, Elegant, and human readable way…

And the Extension functions are,

That’s it! refer my gist for more.

Happy Coding!

--

--

Mohammed Audhil
AndroidPub

Software Engineer @Blackhawk_Networks | more: https://github.com/Audhil (Pray. Eat. Code. Sleep.)