2

有没有办法控制 groovy 的 MarkupBuilder 的输出并过滤掉换行符?我有如下代码:

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.basket(){
    fruit (type:"apple", 1)
    fruit (type:"orange", 2)    
}  

它总是输出:

<basket>
  <fruit type='apple'>1</fruit>
  <fruit type='orange'>2</fruit>
</basket>

我真的很喜欢单行:

<basket><fruit type='apple'>1</fruit><fruit type='orange'>2</fruit></basket>
4

1 回答 1

2

您可以使用 StreamingMarkupBuilder 来做到这一点:

import groovy.xml.StreamingMarkupBuilder

def xml = new StreamingMarkupBuilder().bind {
  basket(){
    fruit (type:"apple", 1)
    fruit (type:"orange", 2)    
  }
}
println xml.toString()

打印出来

<basket><fruit type='apple'>1</fruit><fruit type='orange'>2</fruit></basket>
于 2010-03-30T10:21:22.393 回答