开发者

Groovy XmlSlurper and inserting child nodes

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜