Kotln XML binding DSL
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.idea update Gradle Dec 26, 2017
jdom.examples fix example Jan 2, 2016
jdom Upgrade to Kotlin 1.2.10 Dec 26, 2017
LICENSE readme + license Jan 2, 2016


Build Status

Kotlin XML data binding DSL

This is a tiny library that allows one to generate XML files via Kotlin DSLs. The framework is build upon delegated properties.

Project consists of API module with no dependencies from any XML implementations. There is an implementation module that implements API via JDOM


Apache 2.0


You may download binaries from maven artifacts repository from here http://dl.bintray.com/jonnyzzz/maven

In Gradle build script it may be done like this

repositories {
  maven { url "http://dl.bintray.com/jonnyzzz/maven" }

dependencies {
  compile "org.jonnyzzz.kotlin.xml.bind:jdom:<VERSION>"

Usage example

class Example {
  //declares a property of type String that load data from "ROOT/aaa/ggg/text()"
  var content by JXML / "aaa" / "ggg" / XText

  //same, but for attribute value
  var attribute by JXML / "aaa" / XAttribute("yohoho")

  //parses sub-object fields
  var sub by JXML / "sub" / XSub(Sub::class.java)

  //collection (aka List<>) of strings is loaded here
  var collection by JXML / "parameters" / XElements("param") / "aaa" / XText

  //collection (aka List<>) of sub objects
  var any by JXML / "parameters" / XAnyElements / XSub(Sub::class.java)

class Sub {
  // [2] --- allows to define persist order
  var key by JXML[2] / XAttribute("key")

  // this specifies default value
  var value by JXML[1] / XAttribute("value") - "defaultValue"

  // sometimes a part of XML should be loaded as-is. Type is org.jdom.Element
  var unknown by JXML / XUnknown

///Usage examples
fun test() {
  val e = Example()

  /// save to XML
  val saved = JDOM.save(e)

  /// load from XML
  val copy = JDOM.load(saved, Example::class.java)