Let’s Design a Builder

As an enterprise programmer, it’s hard to escape XML. XML shows up in configuration files and is also used as communication payloads. But no one goes home excited after manually editing XML documents. It’s much worse if XML is embedded as a string within code. Let’s build a DSL that can help programmers to easily create an XML document from data in memory.

Suppose we have a Map with programming languages as keys and authors as values:

 val​ languagesAndAuthors =
  mapOf(​"Java"​ to ​"Gosling"​, ​"Lisp"​ to ​"McCarthy"​, ​"Ruby"​ to ​"Matz"​)

Let’s design a DSL to help programmers translate that data into an XML format, like so:

 <languages>
  <language title=​'Java'​>
  <author>
  Gosling
  </author> ...

Get Programming DSLs in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.