XML to Kotlin Converter - Generate Kotlin Data Classes from XML Online
Generate Kotlin data classes with xmlutil annotations from an XML document.
XML to Kotlin converter, Kotlin data classes, Android XML, Kotlin serialization, XML parsing
How to Convert XML to Kotlin Data Classes - Step by Step Guide
Input Your XML Schema
Start with the XML you want to model. A real response or file works best, because the types are picked from the values in it:
Automatic Kotlin Data Class Generation
The classes appear as you type. Each element with children or attributes becomes one of the Kotlin data classes in the output:
Get Generated Kotlin Code
For the user XML in the example further down, the output looks like this (the import lines at the top are left out here):
Kotlin Code Output
Your XML becomes these Kotlin data classes:
@Serializable
@XmlSerialName("user", "", "")
data class Root(
// attribute id
@XmlSerialName("id", "", "")
val id: String? = null,
@XmlElement(true)
@XmlSerialName("profile", "", "")
val profile: Profile? = null,
)
@Serializable
@XmlSerialName("profile", "", "")
data class Profile(
@XmlElement(true)
@XmlSerialName("firstName", "", "")
val firstName: String? = null,
@XmlElement(true)
@XmlSerialName("lastName", "", "")
val lastName: String? = null,
@XmlElement(true)
@XmlSerialName("age", "", "")
val age: Int? = null,
@XmlElement(true)
@XmlSerialName("verified", "", "")
val verified: Boolean? = null,
)Copy or Download Kotlin Code
Take the code into your project and read your XML with it.
Example: Converting User Data XML to Kotlin
Let's say you have user data from an API in XML format:
<?xml version="1.0" encoding="UTF-8"?>
<user id="USER-001">
<profile>
<firstName>John</firstName>
<lastName>Doe</lastName>
<age>28</age>
<verified>true</verified>
</profile>
</user>The id attribute stays an attribute, profile becomes its own class, and the values pick the types:
@Serializable
@XmlSerialName("user", "", "")
data class Root(
// attribute id
@XmlSerialName("id", "", "")
val id: String? = null,
@XmlElement(true)
@XmlSerialName("profile", "", "")
val profile: Profile? = null,
)
@Serializable
@XmlSerialName("profile", "", "")
data class Profile(
@XmlElement(true)
@XmlSerialName("firstName", "", "")
val firstName: String? = null,
@XmlElement(true)
@XmlSerialName("lastName", "", "")
val lastName: String? = null,
@XmlElement(true)
@XmlSerialName("age", "", "")
val age: Int? = null,
@XmlElement(true)
@XmlSerialName("verified", "", "")
val verified: Boolean? = null,
)What is XML to Kotlin Conversion?
XML to Kotlin conversion turns the shape of an XML document into Kotlin data classes: one class per element that has children or attributes, one property per attribute, child element or text value. That saves you writing the model and its annotations by hand before you can parse a feed, a SOAP response or a config file.
The classes are written for xmlutil on top of kotlinx.serialization, which works on the JVM, on Android and in Kotlin Multiplatform projects.
Frequently Asked Questions
Which library reads the XML into these classes?
kotlinx.serialization has no XML format of its own, so the classes are written for xmlutil (io.github.pdvrieze.xmlutil:serialization), which plugs into it. Add both to your build, then call XML.decodeFromString(Root.serializer(), xmlText). The annotations tell xmlutil which properties are child elements (@XmlElement(true)), which are attributes (no @XmlElement) and which hold an element's text (@XmlValue(true)).
How are the property types picked?
From how the values are written in your XML, across every copy of an element. Whole numbers become Int, or Long once they pass 32 bits, so an ID like 8901240544102066246 is read without losing a digit; anything past 64 bits stays a String. Decimals such as 14.50 become Double, only the exact words true and false become Boolean, and values like 00501, 0x1F or an empty element stay String so nothing is dropped or misread.
How are repeated and nested elements handled?
Every copy of an element is looked at, not just the first one, so a field that only appears in the second <item> still ends up in the class. An element that repeats under one parent becomes a List with an emptyList() default, nested elements get their own data class, and an element with attributes and text keeps both, the text going into a value property.
What happens to names that aren't valid in Kotlin?
The original XML name always stays in @XmlSerialName, so the Kotlin name can be cleaned up freely. publish-date becomes publishDate, keywords like class or fun become classValue and funValue, names starting with a digit get a field prefix, and two names that end up the same get a number. An element called <string> or <list> becomes StringElement or ListElement so it never hides Kotlin's own types.
Can I use the classes in an Android app?
Yes. They are ordinary @Serializable data classes, so they work on Android and in Kotlin Multiplatform as long as the kotlinx.serialization Gradle plugin and xmlutil are set up. The code was checked with Kotlin 2.0, kotlinx.serialization 1.7 and xmlutil 0.90. If you load the XML through Retrofit, you need an XML converter that uses xmlutil rather than the JSON one.
Is my XML uploaded anywhere?
No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server. Very large documents are limited only by your browser's memory and may take a moment to process.