Scala 的人类可读 XML 输出?
Scala 似乎对您输入的 XML 做了两件事,使其可解析性不变,但可读性较差:
首先,它扩展了自身闭合的标签:
scala> <tag/>
res109: scala.xml.Elem = <tag></tag>
其次,它将属性打乱为随机顺序,就好像将它们放入哈希中一样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>
这些共同导致 XML 的可读性大大降低(至少对我来说是这样)。我对 XML 库不是很熟悉;有没有一种方法可以执行 xml 到字符串的转换,从而生成紧凑的人类可读形式? (如果不是默认情况下,通过递归和编写自己的字符串转换 - 或者是否有太多特殊情况潜伏在那里?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数情况下,请参阅 scala.xml.Utility.toXml。不过,属性问题没有解决方案(据我所知)。
您可能还想查看 scala.xml.PrettyPrinter 。
Mostly, see
scala.xml.Utility.toXml
. The attribute thing doesn't have a solution, though (as far as I know).You may want to look at
scala.xml.PrettyPrinter
as well.