I'm probably missing something obvious, since I'm a noob with Groovy, but I searched, but didn't find what I was looking for. I have a test class where I read in XML; I want to insert an element at the beginning of a series of elements. I figured out how to replace the first element, and I figured out how to add a node to the end of the list, but I can't imagine how to insert an element at the beginning of the list (or, ideally, an arbitrary position).
For instance:
@Test
void foo()
{
def xml = """<container>
<listofthings>
<thing id="100" name="foo"/>
</listofthings>
</container>"""
def root = new XmlSlurper().parseText(xml)
root.listofthings.thing[0].replaceNode ( { thing(id:101, name:'bar') })
root.listofthings.appendNode ( { thing(id:102, name:'baz') })
def outputBuilder = new groovy.xml.StreamingMarkupBuilder()
String result = outputBuilder.bind { mkp.yield root }
print result
}
which gives:
<container>
<listofthings>
<thing id='101' name='bar'/>
<thing id='102' name='baz'/>
</listofthings>
</container>
node , .. - replaceNode, 101 100 , , node n- .
(, ? StreamingMarkupBuilder , )
: 1.7.5, Eclipse, .