Groovy XmlSlurper and Insert Child Nodes

Can I use Groovy XMLSlurper and insert child nodes in the index?

The GPathResult class currently has putAt (index) and appendNode (). The first replaces the element in the index, but does not insert, and the second adds to the end.

Unfortunately, I am attached to XmlSlurper, not to XmlParser.

Thank.

+3
source share
1 answer

Found. So simple (crazy awesome groovy). All that is required is to add node after node using closure and the + operator .

For instance:

//Add the ac:MessageStatus after ac:MessageDateTime (this is an Acord message hence the ac:)
root.'ac:MessageDateTime' + {
    'ac:MessageStatus' { 
        'ac:MessageStatusCode'('ac:Success') 
        'ac:SuccessCode'('ac:Success') 
    }
}
+16
source

Source: https://habr.com/ru/post/1792644/


All Articles