Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human-readable XML output from Scala?

Tags:

xml

scala

Scala seems to do two things to XML that you enter that make it no less parseable but make it less readable:

First, it expands tags that close themselves:

scala> <tag/>
res109: scala.xml.Elem = <tag></tag>

And second, it scrambles attributes into random order, as if it put them into a hash set:

scala> <tag a="a" b="b" c="c" d="d"/>         
res110: scala.xml.Elem = <tag d="d" a="a" c="c" b="b"></tag>

Together, these conspire to render XML considerably less human-readable (at least by me). I'm not very familiar with the XML library; is there a way to perform xml-to-string translation that yields a compact human-readable form? (If not by default, by recursing and writing one's own string conversions--or are there too many special cases that lurk there?)

like image 736
Rex Kerr Avatar asked Apr 01 '11 15:04

Rex Kerr


1 Answers

Mostly, see scala.xml.Utility.toXml. The attribute thing doesn't have a solution, though (as far as I know).

scala> xml.Utility.toXML(<a/>, minimizeTags = true)
res13: StringBuilder = <a />

You may want to look at scala.xml.PrettyPrinter as well.

like image 166
Daniel C. Sobral Avatar answered Sep 21 '22 06:09

Daniel C. Sobral