开发者

Human-readable XML output from Scala?

开发者 https://www.devze.com 2023-02-21 17:10 出处:网络
Scala seems to do two things to XML that you enter that make it no less parseable but make it less readable:

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?)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消