Ole*_*ndr 7 xml grails groovy xml-serialization
我正在使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
此代码生成:
<methodResponse>
<resultStatus>
<result>ok</result>
<resultCode>0</resultCode>
<errorString></errorString>
<errorStringLoc></errorStringLoc>
</resultStatus>
</methodResponse>
Run Code Online (Sandbox Code Playgroud)
但我不需要任何标识 - 我只想要一个简单的单行文本:)
ata*_*lor 17
IndentPrinter可以采用三个参数:a 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()
Run Code Online (Sandbox Code Playgroud)
结果:
<methodResponse><resultStatus><result>result</result><resultCode>resultCode</resultCode><errorString>errorString</errorString><errorStringLoc>errorStringLoc</errorStringLoc></resultStatus></methodResponse>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2640 次 |
| 最近记录: |