XML to Kotlin Data Class Generator
Data classes typed from your XML sample
XML Input
Kotlin Output
Paste your XML on the left and the Kotlin code shows up here.
Or drop a file on this box, , or .
What the generator writes
Feed the page an XML document and each distinct element name turns into a Kotlin data class. On the sample, <subscriber> and <device> become Subscriber and Device, and every val carries a trailing comment like // attribute "type" or // <imei> so you can trace it back. Text sitting beside attributes lands in a value property.
Repeated elements become List<T> = emptyList(). Properties missing from some records become nullable with a null default, following Kotlin's null safety rules, and a type that contains itself is always nullable. In the sample, imei holds 15-digit numbers that do not fit in 32 bits, so it is typed Long, while dataUsageGb comes out as Double. Keywords like class are escaped with backticks.
The classes have no annotations, so they do not bind to XML on their own. With jackson-dataformat-xml you will usually add @JacksonXmlProperty(localName = ...) for renamed fields and @JacksonXmlElementWrapper(useWrapping = false) on repeated ones. No server is involved while you work: the Kotlin text is written by script inside this tab.
How to use it
Paste a sample, adjust the options, copy the classes.
Paste a sample
Put XML in XML Input or choose a file with Upload. Two <device> elements inside one subscriber are what produce List<Device>; with only one, the property would be a plain Device. Sample loads a telecom subscriber:
<subscriber id="SUB-1001" active="true">
<plan>Unlimited 5G</plan>
<device type="smartphone"><model>Pixel 8</model></device>
<device type="tablet"><model>iPad Air</model></device>
</subscriber> Bad markup? The Kotlin pane shows the parser error rather than classes, and the XML Validator will point you to the line and column.
Set package and options
Root class renames the outer class (blank keeps Subscriber), and the .kt download takes that name. Package writes the package line and backticks any keyword segment. Data class chooses data class or a plain class. Turn Nullable for missing fields off and optional properties drop their ? = null, so callers must pass them.
Copy or download
Download saves a .kt file named after the root class. Copy puts it on the clipboard.
Where it helps
Android apps on XML APIs
Carrier, banking and travel APIs often still answer in XML. Generated data classes give your repository layer a typed model to map into.
Spring Boot integrations
Paste a partner payload, add the Jackson annotations you need, and you have request and response types for a controller or client.
Replacing manual DOM code
If a codebase pulls values out with getElementsByTagName everywhere, a data class per element is a cleaner target to refactor toward.
Sharing a data contract
A short Kotlin file is easier to review than a long sample document when you agree on a format with another team.
Common questions
Can I deserialize XML straight into these classes?
Not without a library and some annotations. The classes are plain Kotlin. Jackson's XML module or a small hand-written mapper are the usual choices; see the jackson-dataformat-xml README for the annotations.
How are attributes handled?
As ordinary properties, next to the child elements. A comment on each property says whether it came from an attribute, text content or a child element.
How are types inferred?
Every value of the field is checked. If they all fit in 32 bits you get Int; one bigger value, like the IMEIs in the sample, switches it to Long. Decimals give Double and true/false gives Boolean. A value such as 02134 stays String, so a Boston zip code keeps its zero.
What does the Data class option change?
Only the keyword. With it on you get equals, hashCode, toString and copy; with it off you get a plain class with the same properties.
Why do some names have backticks?
XML names such as class, object or in are Kotlin keywords. Backticks let you keep them as property names.
Is my XML uploaded?
No. Parsing and generation both happen in the page itself, without a request to any server. Pastes past 256 KB get a message asking for a smaller sample; one subscriber carrying a couple of devices already shows every property.
Related tools
Other converters you might need next: