When I add a Node
to a NodeList
using the XmlParser
in Groovy, the NodeList
does not appear to reflect the change. Here is the code that I'm testing within the GroovyConsole. If you run this code, you should see the output "ADD FAILED" followed by a dump of the three elements of the NodeList. I've also tried the same thing with XmlSlurper but switched to XmlParser when I read that the DOM in XmlSluper is basically immutable and that changes only get applied during serialize. But with XmlParser, should this work?
def xml = '''
<site id='myCoolSite'>
<pages/>
<templates/>
<properties>
<property name='good' value='true'/>
<property name='nice' value='true'/>
<property name='expensive' value='false'/>
</properties>
<stuff/>
</site>
'''
site = new XmlParser().parseText( xml )
assert site != null
assert site.properties.property instanceof NodeList
assert site.properties.property.size() == 3
def newNode = new Node(null, "property", [name: "foo", value: "bar"] )
site.properties.property.add( 0, newNode )
def foo = site.properties.property.find { it.@name == "foo" }
if ( foo == null ) {
println "ADD FAILED. Here are the only properties found: "
site.properties.property.each { println "${it.@name}=${it.@value}" }
}
else {
println "SUCCESS"
}