What XML to Kotlin does
XML to Kotlin turns a sample XML document into Kotlin code: one data class per object, with a type for every field inferred from the value in the sample.
XML to Kotlin examples
The built-in sample
Input
<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>Output
data class Root(
val _id: String,
val name: String
)This is the input that the Load sample button fills in.
Attributes, repeated elements and empty elements
Input
<order id="7" paid="false"><total>19.5</total><item sku="A1"><qty>2</qty></item><item sku="B2"><qty>1</qty><gift/></item><note/></order>Output
data class Item(
val _sku: String,
val qty: String,
val gift: String
)
data class Root(
val _id: String,
val _paid: String,
val total: String,
val item: List<Item>,
val note: String
)In the sample, id, paid and sku are attributes, item repeats, and gift and note are empty elements. Every value is typed as a string, because XML does not carry types.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Kotlin type:
| Sample value | Kotlin type |
|---|---|
| String | String |
| Whole number | Int |
| Decimal number | Double |
| true / false | Boolean |
| null, or an empty array’s items | Any |
| Array of T | List<T> |
| Object | a new named type |
XML has no data types, so every leaf value is generated as the Kotlin string type. Attributes become fields too: the @ prefix becomes an underscore (_id). Repeated elements become lists, and the root element itself is always called Root.
Reading XML into the generated types
kotlinx.serialization has no XML format in its core library. Either convert the XML to JSON first, or use a separate XML serialisation library and its annotations for attributes.
Details and limits
Any, used for null values and empty arrays, cannot be serialised by kotlinx.serialization. Replace it with the real type or withJsonElement.- A renamed key needs
@SerialName("order-id")on the property. - Properties are non-null
vals. Add?and a default ofnullto the optional ones. - A key that is a Kotlin keyword, such as
class, must be renamed or written in backticks. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.
Questions
Is my data uploaded when I use XML to Kotlin?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does XML to Kotlin do?
XML to Kotlin turns a sample XML document into Kotlin code: one data class per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
Any, used for null values and empty arrays, cannot be serialised by kotlinx.serialization. Replace it with the real type or with JsonElement. A renamed key needs @SerialName("order-id") on the property.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.