在Scala中生成格式化的XML
发布时间:2020-12-16 19:00:39 所属栏目:安全 来源:网络整理
导读:我有一些使用嵌入式 Scala生成的XML,但是它不会将生成的XML放在不同的行上. 目前看来, book id="0" authorGambardella,Matthew/authorpublish_dateSun Oct 01 00:00:00 EDT 2000/publish_datedescriptionAn in-depth look at creating applications with XML
我有一些使用嵌入式
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> 如何控制格式? <book id="0"> { keys map (_.toXML) } </book> 这里是toXML: def toXML:Node = XML.loadString(String.format("<%s>%s</%s>",tag,value.toString,tag)) 解决方法
使用
PrettyPrinter :
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,Null,TopScope,Text(value.toString)) 这可能更快,并删除所有类型的转义问题. (如果value.toString评估为< / a>?) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |