开发者

Groovy XmlSlurper and inserting child nodes

开发者 https://www.devze.com 2023-02-11 03:18 出处:网络
Is it possible to use the Groovy\'s XMLSlurper and insert child nodes at an index? Currently the GPathResult class has putAt(index) and appendNode(). The first replaces the element at the index rathe

Is it possible to use the Groovy's XMLSlurper and insert child nodes at an index?

Currently the GPathResult class has putAt(index) and appendNode(). The first replaces the element at the index rather than inserting and the second adds to the end.

I am tied to XmlSlurper rather than Xml开发者_如何学CParser unfortunately.

Thanks.


Found it. So simple (crazy awesome Groovy). All it takes is to add the node after the node in question using a closure and the + operator.

For example:

//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') 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消