Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
groovy.xml.MarkupBuilder禁用预打印_Xml_Grails_Groovy_Xml Serialization - Fatal编程技术网

groovy.xml.MarkupBuilder禁用预打印

groovy.xml.MarkupBuilder禁用预打印,xml,grails,groovy,xml-serialization,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()

我使用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> 

好啊
0

但是我不需要任何标识-我只需要一行纯文本:)

看看JavaDocs,有一种方法可以设置缩进级别,尽管它不会为您将所有内容放在一行中。也许您可以编写自己的
打印机

可以使用三个参数:一个
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>
resultResultCodeErrorStringLoc

是的,我看到了-我可以禁用识别,但换行符仍然存在。
IndentPrinter
Writer
作为其第一个参数,而不是
PrintWriter
。因此,您可以直接将
writer
传递给它,而不必构造
PrintWriter