从MarkupBuilder结果中删除换行符

时间:2010-03-30 10:11:52

标签: groovy markupbuilder

有没有办法控制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>

1 个答案:

答案 0 :(得分: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>