views:

97

answers:

1

Is there a way to control groovy's MarkupBuilder's output and filter out the newline characters? I have code like below:

import groovy.xml.MarkupBuilder

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

which invariably outputs:

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

I'd really like it in a single line:

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

You can do it with StreamingMarkupBuilder:

import groovy.xml.StreamingMarkupBuilder

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

That prints out

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