AndroidPub
Published in

AndroidPub

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

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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store