Working with XML in Kotlin: Library Overview and Practical Examples
XML (eXtensible Markup Language) remains one of the key formats for storing and transmitting structured data. In the Kotlin language, which runs on top of the JVM, all Java libraries for working with XML are available, as well as several native Kotlin solutions. This article covers three main approaches to working with XML: SimpleXML (annotation-based parsing), XStream (fast serialization), and kotlinx.serialization (native Kotlin library).
1. SimpleXML — Annotation-based XML Parsing
SimpleXML is a popular library for Java/Kotlin that allows you to convert XML to objects and back using annotations. It is ideal when the XML structure is known in advance.
Setup (Gradle):
dependencies { implementation("org.simpleframework:simple-xml:2.7.1")}Example: Parsing an RSS Feed
Suppose we have XML with news:
<rss version="2.0"> <channel> <item> <title>News 1</title> <link>https://example.com/1</link> </item> <item> <title>News 2</title> <link>https://example.com/2</link> </item> </channel></rss>Let's create data models with annotations:
import org.simpleframework.xml.*
@Root(name = "rss", strict = false)data class Rss( @field:Element(name = "channel") var channel: Channel? = null)
@Root(name = "channel", strict = false)data class Channel( @field:ElementList(inline = true, entry = "item") var items: List<Item> = emptyList())
@Root(name = "item", strict = false)data class Item( @field:Element(name = "title") var title: String = "",
@field:Element(name = "link") var link: String = "")Parsing XML:
import org.simpleframework.xml.core.Persister
fun parseRss(xmlString: String): Rss? { return try { val serializer = Persister() serializer.read(Rss::class.java, xmlString) } catch (e: Exception) { println("Parsing error: ${e.message}") null }}
// Usage:val xml = "<rss version=\\"2.0\\">...</rss>" // your XMLval rss = parseRss(xml)rss?.channel?.items?.forEach { item -> println("${item.title}: ${item.link}")}Pros of SimpleXML: simplicity, inheritance support, built-in converters for dates and numbers.
2. XStream — Fast Object Serialization
XStream is another powerful library that can serialize objects to XML without annotations. It uses reflection and is well-suited for prototyping.
Setup:
dependencies { implementation("com.thoughtworks.xstream:xstream:1.4.20")}Example: Serialization and Deserialization
import com.thoughtworks.xstream.XStream
data class Person(val name: String, val age: Int)
fun main() { val xstream = XStream() xstream.alias("person", Person::class.java)
// Serialization val person = Person("Ivan", 30) val xml = xstream.toXML(person) println(xml) // Output: <person><name>Ivan</name><age>30</age></person>
// Deserialization val restored = xstream.fromXML(xml) as Person println("Name: ${restored.name}, Age: ${restored.age}")}Features of XStream:
- Does not require annotations — works "out of the box"
- Supports collections, arrays, nested objects
- Can configure aliases for classes and fields
- Eat