• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Baron
Ranch Hand
Posts: 1061
  • 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
  • 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
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jon !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic