8

There are numerous Scala libraries to convert arbitrary objects to JSON and a few to convert JSON to XML, but I can't seem to find a nice way to convert arbitrary objects to XML. What's a good approach?

Josh
  • 875
  • 1
  • 13
  • 25

3 Answers3

3

I used scalaxb a while ago. It works very well if you have an XSD (or WSDL) as input to generate your classes. If this is the case, I also recommend using it.

Since you're also talking about JSON it may be that you have a web application using REST. In this case, have a look at Lift's REST-Support. For instance, if you use one of its internal persistency frameworks then all your entity types get a toXML method for free.

A third possibility would be to write your own conversion using Scala's XML literals.

Btw possible duplicates on Stack Overflow: Scala XML serialization and Which XML serialization library for Scala?

Community
  • 1
  • 1
Silas
  • 1,140
  • 11
  • 12
  • Liftweb's toXml looks like it takes a JValue, not an arbitrary object, unless there's some part of the API that I'm missing. Re: scalaxb, I don't have schemas. – Josh Dec 03 '11 at 22:49
  • Sorry, I was talking about Lift's persistency entities with a mixed in trait [Mapper](http://scala-tools.org/mvnsites/liftweb-2.4-M4/#net.liftweb.mapper.Mapper). – Silas Dec 04 '11 at 12:01
1

Check this project: https://github.com/geirolz/advxml

It's a simple library based on RuleTransformer(standard scala xml library) and Cats with the aim to simplify the XML transformation and serialization/deserialization.

David Geirola
  • 616
  • 3
  • 17
0

scalaxb looks decent, I'd try that first.

Alex Cruise
  • 7,939
  • 1
  • 27
  • 40