xml - Gradle zip如何为XML操作添加过滤器

标签 xml gradle filter

Gradle zip:如何通过添加新节点来过滤XML文件,例如,

task mytask(type: Zip) {

    from ("foo/bar") {

        include "config.xml"
        filter {
             def root = new XmlParser().parser(configXml_inputStream)
             root.hello.world.append(aNode)
             groovy.xml.XmlUtil.serialize(root, configXml_outputStream)
        }
    }

}

过滤器关闭参数是一行,而不是文件。如何编写自定义过滤器来处理XML文件
filter(myFilterType)

找不到有关创建自定义过滤器的示例/文档。

最佳答案

过滤器在行上工作,而不是xml节点。以下示例说明了使用行的替换,但请注意,这是xml的一种奇怪方法,在一般情况下不起作用。

鉴于此foo/bar/config.xml:

<root>
    <hello>
        <world>
        </world>
    </hello>
</root>

假定仅要增加一个<world>元素,然后考虑此build.gradle:
task mytask(type: Zip) {
    archiveName "config.zip"

    from ("foo/bar") {
        include "config.xml"

        filter { line ->
            def result = line

            if (line.trim() == '<world>') {
                def buffer = new StringBuilder()
                buffer.append(line + "\n")
                buffer.append('<aNode type="example">' + "\n")
                buffer.append('</aNode>')
                result = buffer.toString()
            }

            result
        }
    }
}

那么config.xml中的config.zip是:
<root>
    <hello>
        <world>
<aNode type="example">
</aNode>
        </world>
    </hello>
</root>

关于xml - Gradle zip如何为XML操作添加过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45013900/

相关文章:

xml - 首选项中缺少 Eclipse XSL 选项

php - 使用 php 从 mysql 数据库创建一个 xml 文件

javascript - 使用 Google Apps 脚本对 eBay API 的 XML 请求返​​回 'The API call "GeteBayOfficialTime“无效或在此版本中不受支持”错误

gradle - Gitlab CI - 将失败的测试结果发布到页面

gradle - 自定义插件中的默认操作

javascript - 为什么这个简单的过滤器返回一个空数组?

c# - 在 C# 中使用 RegEx 在其他项出现后查找尖括号内的第一项

java - 使用Gradle进行时间点构建

elasticsearch - 未将过滤器应用于聚合

google-apps-script - 如何从过滤器中自动选择范围而无需手动输入?