aldrin
aldrin

Reputation: 4572

Remove newlines from MarkupBuilder result

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>

Upvotes: 2

Views: 1724

Answers (1)

tim_yates
tim_yates

Reputation: 171084

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>

Upvotes: 2

Related Questions