我使用groovy.xml.MarkupBuilder来创建XML响应,但是它创建了漂亮的打印结果,这在生产中是不需要的。
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def cities = cityApiService.list(params)
xml.methodResponse() {
resultStatus() {
result(cities.result)
resultCode(cities.resultCode)
errorString(cities.errorString)
errorStringLoc(cities.errorStringLoc)
}
}这段代码产生:
<methodResponse>
<resultStatus>
<result>ok</result>
<resultCode>0</resultCode>
<errorString></errorString>
<errorStringLoc></errorStringLoc>
</resultStatus>
</methodResponse> 但我不需要任何标识-我只需要一个纯单行文本:)
发布于 2010-07-17 00:00:35
IndentPrinter可以接受三个参数:PrintWriter、缩进字符串和布尔型addNewLines。您可以通过使用空缩进字符串将addNewLines设置为false来获得所需的标记,如下所示:
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def xml = new MarkupBuilder(new IndentPrinter(new PrintWriter(writer), "", false))
xml.methodResponse() {
resultStatus() {
result("result")
resultCode("resultCode")
errorString("errorString")
errorStringLoc("errorStringLoc")
}
}
println writer.toString()结果是:
<methodResponse><resultStatus><result>result</result><resultCode>resultCode</resultCode><errorString>errorString</errorString><errorStringLoc>errorStringLoc</errorStringLoc></resultStatus></methodResponse>发布于 2010-07-16 23:19:17
看看JavaDocs,在IndentPrinter上有一个方法可以设置缩进级别,尽管它不会把所有的缩进级别都放在一行中。也许您可以编写自己的Printer
https://stackoverflow.com/questions/3266115
复制相似问题