This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-nested XML-Tag with attributes using groovy.xml.MarkupBuilder

 
Chris Baron
Ranch Hand
Posts: 1061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
a question about Groovy's MarkupBuilder:
how to generate non-nested XML-Tag with attributes?

I'd expect a syntax like


But that doesn't work.
Thanks cb
 
Jon Dickinson
Author
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

The following should work:



The key being mytag(id:"1", "Gnnrf")

Cheers,
Jon.
 
Chris Baron
Ranch Hand
Posts: 1061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yip it works. Thanks Jon!

Could you please explain, why in this case one doesn't use a closure but a hmm... method-parameter?

Thanks cb
 
Jon Dickinson
Author
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all down to the createNode methods on the BuilderSupport class in Groovy.



Every method call you make on a builder must map to one of the createNode methods on the BuilderSupport class. So the available options are:



Where myNode = name in the createNode method.

To answer your question, it's down to the implementation of the MarkupBuilder, it uses the value argument as text content of the current node.

Here's the overview of builders from the Groovy site: http://groovy.codehaus.org/Builders. Here's an article I wrote on builders when I was starting to get into Groovy, if your interested: http://javaboutique.internet.com/tutorials/gbuilder/

Cheers,
Jon.
 
Chris Baron
Ranch Hand
Posts: 1061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jon !
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic