Admittedly I really love the Jerkson library because it makes generating JSON so easy. Take for example:
Json(generate(Job.search(parseDate(date),accountId)
.map(job => Map("id" -> job.id,
"name" -> job.name,
"userId" -> job.userId.getOrElse("")
))))
But I am having difficulty finding a similar library that could generate and serialize XML nodes, etc., as easily as this. Is there a Java or Scala lib that could generate the nodes and values in a simple syntax without having to make “templates” or manually write the nodes?
It’s much more difficult to build XML than JSON, because of the “double hierarchy” of attributes and children nodes. JSON structure is easily represented with standard Scala collections, by nesting maps and lists.
But if your are just interested in a subset of XML, the problems is less complex. According to your answer to my comment, the following snippet could be enough for what you need:
It works as expected. For instance:
Then
xmlwill be equal to:It’s just a quick and dirty hack, but I think it can be easily improved (to include lists for instance) and made safer.