xml - Groovy XmlSlurper 和插入子节点

标签 xml groovy

是否可以使用 Groovy's XMLSlurper并在索引处插入子节点?

目前 GPathResult 类有 putAt(index) 和 appendNode()。第一个替换索引处的元素而不是插入,第二个添加到末尾。

不幸的是,我依赖于 XmlSlurper 而不是 XmlParser。

谢谢。

最佳答案

找到了。如此简单(非常棒 Groovy)。只需使用闭包和 + 运算符在相关节点之后添加节点即可。

例如:

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

关于xml - Groovy XmlSlurper 和插入子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5022353/

相关文章:

java - 无法读取架构文档 'http ://www. springframework.org/schema/beans/spring-beans.xsd

java - 基于 Java 服务器的 Web 应用程序中的 HTTP 状态代码

unicode - 无法理解如何让 Groovy 输出我的 unicode 字符串

xml - Groovy > 嵌套映射到 Xml

grails - 使用Groovy将String或String []转换为列表

jdbc - Groovy 不读取 .groovy/lib 中的 jar 文件

c# - Linq to XML 具有 n 个动态 where 条件

xml - 简单的前同级选择返回意外结果

android - NavigationView 和 Toolbar 被内容覆盖

google-app-engine - 在 GAELYK 中,我如何将表单提交的参数变量存储为实体/数据存储中的文本类型