CalcCafe

XML to Kotlin

Paste XML and instantly generate idiomatic Kotlin data classes with inferred types and nested structures.

Example

Input XML:

<book id="1" inStock="true">
 <title>Kotlin in Action</title>
 <year>2017</year>
 <price>39.99</price>
</book>

Generated Kotlin:

data class Root(
  val id: Int,
  val inStock: Boolean,
  val title: String,
  val year: Int,
  val price: Double
)

How it works

The tool parses your XML with the browser DOMParser, walks the element tree to infer a type model (scalars, nested objects, repeated elements as lists), then emits Kotlin data classes with the root type named Root. Everything runs locally with no network calls.

Good to know

XML to Kotlin turns a pasted XML document into ready-to-use Kotlin data class definitions, inferring property types and nested object structures from the actual data you give it. It is aimed at Android and Kotlin/JVM developers who receive XML from a legacy API, a config file, an RSS/Atom feed, or a SOAP response and want a typed model to deserialize into instead of hand-writing classes field by field.

Reach for it whenever you have a representative XML sample but no schema (no XSD or generated stubs). Because the tool infers types from the literal values it sees, the richer your sample, the better the output: include every optional field, at least two of any repeated element, and realistic numbers so it can tell an Int from a Double. The root type is always emitted as Root, and each nested element becomes its own class named after the tag.

To read the result, scan the property types as the tool's interpretation of your data. A plain type like val year: Int means that element appeared everywhere with an integer value; List<String> means the element repeated under its parent; and a trailing ? (nullable) means the field was present in some siblings but missing in others. A typealias Root = ... line instead of a class means your XML root holds a single scalar value rather than child elements.

One practical caveat: this is structure-and-type inference, not a serialization-framework binding. The generated classes are POJO-style Kotlin and do not include annotations for any specific XML library (such as Jackson XML, Simple XML, or kotlinx-serialization), so you will need to add those yourself. Also note that XML attributes are flattened into ordinary properties alongside child elements, so an attribute and a child element with the same name in the same parent can collide.

Frequently asked questions

How are repeated XML elements handled?
When the same child element appears more than once under a parent (like multipletags), it is emitted as a Kotlin List, for example val author: List. Fields missing in some siblings become nullable.
How are XML attributes and types converted?
Attributes become regular properties on the generated data class alongside child elements. Values are inferred as Int, Long, Double, Boolean, or String, and conflicting numeric types are widened (Int + Double becomes Double).
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.

People also ask

Does the generated Kotlin work with Jackson XML or kotlinx-serialization?
The output is plain Kotlin data classes without any serialization annotations, so it is library-agnostic. You can use it as-is with reflection-based mappers, or add @JacksonXmlProperty, @Element, or @Serializable annotations manually depending on your chosen library.
Why did a field come out as nullable when my XML always has it?
A property is marked nullable when the same element is missing from at least one sibling in your sample. If a field is always required in reality but absent from one example, add a complete sample so every occurrence includes it, or remove the question mark by hand.
How does the tool decide between Int, Long, and Double?
It checks each value's text: whole numbers within the 32-bit range become Int, larger whole numbers become Long, and numbers with a decimal point become Double. When the same field shows both an Int and a Double across the data, the type is widened to Double.
Can it convert an XSD or DTD schema instead of sample XML?
No. It infers the model purely from a concrete XML instance using the browser's DOMParser, so it does not read XSD, DTD, or RELAX NG schema files. You would need an actual XML document with representative values.
What happens to XML attributes in the output?
Attributes are treated as regular properties on the data class for that element, sitting next to the properties derived from child elements. Their values are type-inferred the same way as element text.
Is there a size limit on the XML I can paste?
There is no hard limit imposed by the tool, and everything runs locally in your browser. Very large documents are bounded only by your device's memory and the browser's DOMParser, since no data is sent to a server.
Why is my output just a typealias instead of a data class?
That happens when the XML root element contains a single scalar value rather than child elements or attributes. The tool emits a typealias to that inferred type because there are no fields to form a class.
How are namespaced or prefixed XML tags handled?
Tag and attribute names are sanitized into valid Kotlin identifiers by stripping non-alphanumeric characters and converting to camelCase, so a prefix separator like a colon is removed. This means two differently namespaced tags that reduce to the same name could merge, so review prefixed elements in the output.

Related tools