26 May 2004 15:40
Re: about MarkupBuilder
On 26 May 2004, at 13:05, steb wrote:
> Hi,
>
> I would like to know what is the best groovysh way to create and
> update an xml file, say like the example taken from Mark Volmann's
> article :
> import groovy.xml.*;
>
> mb = new MarkupBuilder()
> text = mb.html() {
> head() {
> title("This is my title.")
> }
> body() {
> p("This is my paragraph.")
> }
> }
>
> I don't understand why "text" equals to "html" (as mentioned on
> http://raibledesigns.com/page/rd). So how to get the complete xml
> string?
MarkupBuilder outputs the markup as text to some Writer. It does not
presume to capture in some String buffer somewhere the output thats
occurred on each method call. If thats what you want to do, you could
use a StringWriter
buffer = new StringWriter()
mb = new MarkupBuilder(buffer) // new helper constructor just added to
CVS
mb.html { head { } body { p("hey" } }
text = buffer.toString()
> How would you add a paragraph to body or change its value?
The script which works on the markup builder instance creates the
content. So you can use normal groovy script to generate whatever code
you wish.
for (x in 1..10) {
p("some text ${x}")
}
etc
> Would you use the XMLParser but then, how to write back to the file?
The purpose of GroovyMarkup and builders is to generate a bunch of
markup and stream it somewhere (SAX events, DOM trees, beans, text
file, Ant tasks etc).
I suspect what you really wanna do is operate on some DOMish tree
structure then write it out again somewhere? If so there's a
NodeBuilder you can use to make a tree of Nodes and then you can
navigate and modify the nodes and then output it again.
James
-------
http://radio.weblogs.com/0112098/
RSS Feed