Thursday, February 28, 2008

Groovy's MarkupBuilder and Namespaces

Yesterday Kit Plummer posted an XML example using Ruby to demonstrate how to include attributes and namespaces. So I went on a quest to duplicate his output using Groovy to compare the difference and evangelize.

Here is the desired output:

<person:person text='test'>
<name>Jim</name>
<phone>555-1234</phone>
</person:person>
Using the Groovy Console, and Groovy's ninja-like MarkupBuilder, you can execute the following script to get attributes and namespace prefixes:
import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.'person:person'(text: 'test') {
name('Jim')
phone('555-1234')
}

println writer.toString()

If you prefer to define a default namespace instead do this:
xml.person(xmlns: 'http://people.org', text: 'test') {
name('Jim')
phone('555-1234')
}
Finally, here is an example that uses them all:
xml.'person:person'('xmlns:person': 'http://people.org', xmlns: 'http://people.org', text: 'test') {
name('Jim')
phone('555-1234')
}

This will output:
<person:person xmlns:person='http://people.org' xmlns='http://people.org' text='test'>
<name>Jim</name>
<phone>555-1234</phone>
<person:person>
I think it's more readable than the Ruby example and definitely much better than the equivalent Java code.

By the way, I attempted to use this online syntax highlighter and did not like it. Does anyone know of a good online syntax highlighter?

5 comments:

joshuanhoover said...

You know deep down inside that Ruby is the language you desire James. ;)

...And while I'm thinking about programming language wars, I must link to this:

http://davidrupp.blogspot.com/2007/10/last-language-war-language-trolling.html

kitplummer said...

Good stuff James.

I actually like your solution.

For as much as I'm diggin' on Ruby these days - it definitely falls flat is a few areas. XML processing is one of those.

If I had a reason to - I'd like to do some work with Groovy.

Anonymous said...

Now do you worried about that in the game do not had enough Scions Of Fate gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot SOF gold and only spend a little money, do not hesitate, it was really, in here we had much Scions Of Fate money, we can sure that you will get the cheap SOF gold, quick to come here to buy sof gold.

Now do you worried about that in the game do not had enough seal cegel to play the game, now you can not worried, my friend told me a website, in here you can buy a lot sealonline cegel and only spend a little money, do not hesitate, it was really, in here we had much seal online cegel, we can sure that you will get the cheap seal cegel, quick to come here to buy seal online cegel.

products said...

China Wholesale has been described as the world’s factory. This phenomenom is typified by the rise ofbusiness. Incredible range of products available with China Wholesalers “Low Price and High Quality” not only reaches directly to their target clients worldwide but also ensures that wholesale from china from China means margins you cannot find elsewhere and buy products wholesaleChina Wholesale will skyroket your profits.wedding dressescheap naruto cosplayanime cosplaycheap Gemstone Jewelry

products said...

Women’s nike tn Shox Rivalry est le modèle féminin le plus tendance de baskets pour le sport. tn chaussuresConcernant la semelle :spyder jacketsCheap Brand Jeans Shop - True Religion Jeans cheap nike shox & Puma Shoes Online- tn nike,Diesel Jeans le caoutchouc extérieur, l’EVA intermédiaire Levis Jeanset le textile intérieur s’associent pour attribuer à la.ed hardy shirts pretty fitCharlestoncheap columbia jackets. turned a pair of double plays to do the trick.Lacoste Polo Shirts, puma basket, Burberry Polo Shirts.wholesale Lacoste polo shirts and cheap polo shirtswith great price.Thank you so much!!cheap polo shirts men'ssweate,gillette mach3 razor bladesfor men.As for Cheap Evisu JeansCheap Armani Jeanspolo shirtsPuma shoes