groovy.xml.MarkupBuilder禁用PrettyPrint [英] groovy.xml.MarkupBuilder disable PrettyPrint

查看:123
本文介绍了groovy.xml.MarkupBuilder禁用PrettyPrint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用groovy.xml.MarkupBuilder来创建XML响应,但是它创建了不需要的生成结果。

  def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def cities = cityApiService.list(params)

xml.methodResponse(){
resultsStatus(){
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>

但我不需要任何标识 - 我只想要一个简单的单行文本:)

解决方案

IndentPrinter 可以取三个参数:一个 PrintWriter ,一个缩进字符串和一个布尔值 addNewLines 。您可以通过将 addNewLines 设置为false并使用空缩进字符串来获得所需的标记,如下所示:

  import groovy.xml.MarkupBuilder 
$ b $ def writer = new StringWriter()
def xml = new MarkupBuilder(new IndentPrinter(new PrintWriter(writer), ())

xml.methodResponse(){
resultStatus(){
result(result)
resultCode(resultCode)
errorString(errorString)
errorStringLoc(errorStringLoc)
}
}

println writer.toString()



结果:

 < methodResponse> < resultStatus><导致>导致< /导致><发送resultCode>发送resultCode< /发送resultCode>< Errorstring,则> Errorstring,则< / Errorstring,则>< errorStringLoc> errorStringLoc< / errorStringLoc>< / resultStatus>< / methodResponse> 


I'm using groovy.xml.MarkupBuilder to create XML response but it creates prettyprinted result which is unneeded in production.

        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)
            }
}

This code produces:

<methodResponse> 
  <resultStatus> 
    <result>ok</result> 
    <resultCode>0</resultCode> 
    <errorString></errorString> 
    <errorStringLoc></errorStringLoc> 
  </resultStatus> 
</methodResponse> 

But i don't need any identation - i just want a plain one-row text :)

解决方案

IndentPrinter can take three parameters: a PrintWriter, an indent string, and a boolean addNewLines. You can get the markup you want by setting addNewLines to false with an empty indent string, like so:

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()

The result:

<methodResponse><resultStatus><result>result</result><resultCode>resultCode</resultCode><errorString>errorString</errorString><errorStringLoc>errorStringLoc</errorStringLoc></resultStatus></methodResponse>

这篇关于groovy.xml.MarkupBuilder禁用PrettyPrint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆