XML to Kotlin Converter Online — Generate Data Class

Paste any XML and generate @Serializable Kotlin data classes with XmlUtil annotations. Attributes use @XmlElement(false), elements use @XmlSerialName, and nested structures get their own data classes — all in your browser.

What is an XML to Kotlin converter?

An XML to Kotlin converter takes a real XML document and emits annotated data class definitions that the kotlinx-serialization-xml engine (XmlUtil) can decode. The OpenFormatter generator inspects every element and attribute, infers Kotlin types, and produces a complete data class hierarchy with @Serializable, @XmlSerialName, @XmlElement(false), and @XmlValue — saving the tedious step of writing them by hand for SOAP, RSS, or Android XML APIs.

XML differs from JSON in two ways your Kotlin model has to honour. First, XML has attributes on elements (<book id="bk101">) — these need @XmlElement(false) so XmlUtil reads them as attributes, not child elements. Second, XML has namespaces; the basic generator strips them, but you can re-add them as the namespace argument on @XmlSerialName. Both aspects are handled by the sample below.

Sample XML and the Kotlin data class it generates

Input XML

<book id="bk101" lang="en">
  <title>Kotlin in Action</title>
  <year>2017</year>
  <price currency="USD">44.99</price>
</book>

Generated Kotlin

@Serializable
@XmlSerialName("book")
data class Book(
    @XmlSerialName("id") @XmlElement(false)
    val id: String,
    @XmlSerialName("lang") @XmlElement(false)
    val lang: String,
    @XmlSerialName("title")
    val title: String,
    @XmlSerialName("year")
    val year: Int,
    @XmlSerialName("price")
    val price: Price
)

Notice id and lang use @XmlElement(false) to mark them as attributes, while title and year are plain elements. The nested <price currency="USD">44.99</price> generates a Price data class with one attribute property and one @XmlValue for the text content — exactly what XmlUtil expects.

How to convert XML to Kotlin — 4 steps

  1. Paste your XML. A SOAP envelope body, an RSS feed entry, an Android resource — anything well-formed.
  2. Click Convert. The browser parses the XML and generates @Serializable data classes for the full element tree.
  3. Review attribute mapping. Confirm @XmlElement(false) appears on every property that should be an XML attribute; tweak property names if needed.
  4. Drop into your project. Copy the output into a .kt file, then decode real XML with XML().decodeFromString<Root>(xml).

XmlUtil Annotations

Generates @Serializable data classes with @XmlSerialName plus @XmlElement(false) for attributes and @XmlValue for text — ready for kotlinx-serialization-xml.

Attribute vs Element

XML attributes get @XmlElement(false); child elements get @XmlSerialName only. The distinction is preserved exactly so round-tripping is lossless.

Client-Side Only

Your XML — including SOAP responses with credentials — is parsed in JavaScript on your machine. Verify in DevTools: zero network requests on Convert.

Common use cases

  • check_circleGenerate Kotlin data classes from a SOAP service response
  • check_circleBuild Android models for an RSS or Atom feed reader
  • check_circleParse Android XML resources (strings, layouts) into data classes
  • check_circleGenerate Kotlin types from an XSD-described XML file
  • check_circleCreate decoder models for legacy enterprise XML
  • check_circleBuild typed data classes for a SAML or WS-Security XML payload
  • check_circleGenerate Kotlin Multiplatform models that work on JVM, JS, and native
  • check_circleCreate data classes for sitemaps.xml or robots.xml

Why client-side conversion matters

SOAP responses and enterprise XML often contain customer PII, API tokens, internal endpoint names, and database identifiers. Pasting them into a server-hosted converter is a compliance violation in most regulated industries. OpenFormatter parses the XML and generates Kotlin code entirely in JavaScript on your device — no upload, no logs, no cookies. Open DevTools → Network and confirm no requests fire when you click Convert.

More XML tooling

Validate, format, or convert XML to other languages — every tool runs locally in your browser.

Frequently Asked Questions

Does the output use kotlinx-serialization-xml or JAXB?

The default output uses kotlinx-serialization-xml via the XmlUtil library (nl.adaptivity.xmlutil:xmlutil-serialization). It pairs @Serializable data classes with @XmlSerialName, @XmlElement(false) for attributes, and @XmlValue for text content. JAXB is JVM-only and does not work on Kotlin Multiplatform — XmlUtil works on JVM, JS, and native targets.

Can the same data class also be used with JAXB on the JVM?

Yes — Kotlin data classes are valid Java classes. Add JAXB annotations alongside the kotlinx ones (@XmlRootElement, @XmlElement, @XmlAttribute) and provide a no-arg constructor with all-default parameters or use @JvmOverloads. JAXB will read its annotations; XmlUtil ignores them.

How are XML attributes mapped to Kotlin properties?

XML attributes (e.g. <book id="bk101">) become val properties annotated with @XmlSerialName("id") @XmlElement(false). The @XmlElement(false) marker tells the XmlUtil decoder this property is an attribute, not a child element. Element children get @XmlSerialName only.

How are repeated elements like <tag> in <tags> represented?

Repeated child elements become a List<TagType> property on the parent data class. XmlUtil unwraps each repeated element into one list entry. For wrapped lists (an outer <tags> containing inner <tag>) add @XmlChildrenName("tag") on the list property.

What about XML namespaces?

Namespace-qualified elements use the namespace argument: @XmlSerialName(value = "book", namespace = "http://example.com/ns", prefix = "ex"). The basic generator strips namespaces; for namespaced binding rewrite the annotation with all three arguments and XmlUtil will match exactly.

Can elements with both attributes and text content be modeled?

Yes. <price currency="USD">44.99</price> generates a Price data class with two properties — currency annotated with @XmlSerialName("currency") @XmlElement(false) and a value field annotated with @XmlValue. XmlUtil decodes the text body into the @XmlValue property while reading the attribute separately.

Is the XML I paste sent to your servers?

No. XML is parsed by the browser DOMParser and the Kotlin code is generated entirely in JavaScript on your machine. Open DevTools → Network and you will see no requests when you click Convert. Safe for SOAP responses and configuration files containing API keys or credentials.

How do I decode XML into the generated data class?

Use XmlUtil: val xml = XML { defaultPolicy { ignoreUnknownChildren() } }; val book = xml.decodeFromString<Book>(xmlString); — the annotated properties are populated automatically. Add the dependency io.github.pdvrieze.xmlutil:serialization-jvm (or -android, -js) and apply the kotlinx-serialization plugin.

XML to Kotlin Converter — Generate Data Class