Groovy Markup Builder
Comments:

I'm not sure I'm fully following what you're trying to do, but if you've already got a map, why are you trying to recreate it using "attributes.each{key, value -> key:value}"?

In the markup builder, there's a magic value called "mkp" that you can call yield on to print a value out as the text node child of an XML node.

Here's an example that approximates what I think it is you're trying to do:

import groovy.xml.MarkupBuilder
import java.text.SimpleDateFormat

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
def sdf = new SimpleDateFormat("hh:mm aa z EEEE, MMM d")
def updateTime = new Date()

def attributes = [day: "2", mon:"8", date:"11", yr:"2008", hr: "4", tz: "GMT", mer:"am", min:"1", sec:"0"]

builder.updatetime(attributes) {
mkp.yield sdf.format(updateTime)
}

writer.toString()


prints:


07:12 PM CDT Wednesday, Apr 8

Posted by Ted Naleid on April 08, 2009 at 08:32 PM CDT
Website: http://naleid.com/blog #

Shoot, the comment system ate the html part of the result even though it was in a pre. Give it a try in a groovyConsole as I think the output is the same as what you wanted in your post.

Posted by Ted Naleid on April 08, 2009 at 08:38 PM CDT
Website: http://naleid.com/blog #

I can just pass the map in and get the attributes as the key="value" pairs? Awesome, I don't think I've seen that mentioned anywhere. I'll be trying that today. Thanks Ted.

Posted by Lance (76.17.209.80) on April 09, 2009 at 07:47 AM CDT #

Okay, I had a "DOH" moment: I'm still relatively new to groovy and it escaped me that builder.x(foo:'bar', bodyText) is the same as builder.x(fooHash, bodyText)! builder.updatetime(attributes, sdf.format(updateTime)) worked just fine.

Posted by Lance (76.17.209.80) on April 09, 2009 at 08:26 AM CDT #

Comments have been disabled.