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