I like creating DSLs in Kotlin. It was fun to try creating a DSL for XML data binding. Of course there are so many other libraries for JVM that implements it in the other way.
I was looking for a way to use XPath like queries for data binding. I did not want to parse queries from raw strings. The DSL inherited some ideas from XPath queries.
In Kotlin I used Delegated Properties to achieve better readability, avoid explicit type names for serialization/deserialization rules.
Let’s consider an example. Say you have an XML:
Thanks to the kotlin.xml.bind, you may parse it with the following DSL in Kotlin:
Kotlin compiler infers types for properties, so one should not write types at all.
In the example above
name properties are
XAnyElements means result will be as collection,
parses sub-elements as
kotlin.xml.bind provides serialization and deserialization.
Sources / Binaries
Sources are on GitHub under Apache 2.0 license. Binaries are published to a maven repo.
For more information, see kotlin.xml.bind project on GitHub.
I use JDOM to work with XML. DSL is done in separate module that does not depend on JDOM directly.
The main trick is that expressions like
JXML / "keys" / XElements("key") / XText returns an object
that implements requirements of
The library adds a tiny overhead on every object creation. So now to create an object (e.g.
Project class above)
it has to create a number of objects from the DSL that are used behind delegated properties. Reflections
are still in use to scan available object properties too on serialization/deserialization.
For tests I created yet another DSL to generate XML easily. You may find more details in kotlin.xml.dsl on GitHub. This is an example of unit test
The ides of the DSL are easily mapped to JSON too. So it’s possible to read/write/update JSON files in the same way
Get rid of reflections and use Companion Objects
Support other XML implementations, not only JDOM
Improve, finalize and document binding DSL
Have more fun!