Friday, 19 September 2014

Groovy add to XML before saving



How can I add to XML something like this? I know, that there are http://ift.tt/Xu8QZm and http://ift.tt/1tw0NrL but, I don't need it. I will have something like this:



<logger>
<config name="test1" level="ERROR">
<pack>com.simple.test</pack>
...
</config>
<config name="test2" level="INFO">
<pack>com.simple.test</pack>
...
</config>
<config name="test2" level="INFO">
...
</logger>


I have loop in loop. One loop create one config with name and level, the second loop adds pack with strings like "com.simple.test" And I will do this only once. So I think, that no sense to create file, fill it, close, open, update, close, update... and it will do this loop1_steps * loop2_steps. Now I'm doing this that way, but don't understend how to update it before saving file. So, I want first create full xml, and save it next.



import groovy.xml.MarkupBuilder

def writer = new FileWriter(new File( "C:\\dev\\test.xml" ))
def xml = new MarkupBuilder(writer)
xml.logger() {
config(name:'test1', level:'ERROR') {
pakiet('com.simple.test')
}
config(name:'test2', level:'INFO') {
pack('com.simple.test')

}
}


Thanks!


No comments:

Post a Comment