我有一些使用嵌入式Scala生成的XML,但它并没有将生成的XML放在不同的行上。
目前,它看起来像这样,
<book id="0">
<author>Gambardella, Matthew</author><publish_date>Sun Oct 01 00:00:00 EDT 2000</publish_date><description>An in-depth loo
k at creating applications with XML.</description><price>44.95</price><genre>Computer</genre><title>XML Developer's Guide</title>
</book>
但我希望它看起来像这样:
<book id="0">
<author>Gambardella, Matthew</author>
<publish_date>Sun Oct 01 00:00:00 EDT 2000</publish_date>
<description>An in-depth look at creating applications with XML.</description>
<price>44.95</price>
<genre>Computer</genre>
<title>XML Developer's Guide</title>
</book>
如何控制格式? 以下是生成XML
的代码<book id="0">
{ keys map (_.toXML) }
</book>
这里是XML:
def toXML:Node = XML.loadString(String.format("<%s>%s</%s>", tag, value.toString, tag))
答案 0 :(得分:17)
val xml = // your XML
// max width: 80 chars
// indent: 2 spaces
val printer = new scala.xml.PrettyPrinter(80, 2)
printer.format(xml)
顺便说一句,您可能需要考虑将toXML
替换为:
def toXML: Node = Elem(null, tag, Null, TopScope, Text(value.toString))
这可能更快,并消除所有类型的转义问题。 (如果value.toString
评估为</a>
,该怎么办?)