除了XmlSlurper和XmlParser之外,Jenkins管道中是否还有其他一些解析xml文件的方法

insrf1ej  于 2022-12-03  发布在  Jenkins
关注(0)|答案(1)|浏览(194)

我有一个如下所示的Servers.xml文件

<Servers>
    <Environment id="1" name="Jenkins">
        <Server id="1" ip="192.168.0.1" />
        <Server id="2" ip="10.21.22.30" />
    </Environment>

    <Environment id="2" name="Pipe">
        <Server id="1" ip="192.10.2.77" />
        <Server id="2" ip="122.30.45.99" />
    </Environment>

</Servers>

我想根据环境标签name更改ip的值。我搜索了一些答案,但大多数答案都使用XmlSlurperXmlParser,不幸的是,Jenkins管道中不允许使用这两种方法,我得到了

Scripts not permitted to use new groovy.util.XmlSlurper. Administrators can decide whether to approve or reject this signature.

我的问题是,除了XmlSlurperXmlParser是否有其他方法可以解析管道中的xml文件
我没有权限允许以上两种方式,我也没有权限下载插件。
谢谢你,谢谢你
我试过了

def fileContent = readFile xmlFile
// def xml = new XmlParser().parseText(fileContent)
// def xml = new XmlSlurper().parseText(fileContent)
xml.Environment.each {...}

@NonCPS
String extractFromXml(String xml) {
    def node = new XmlSlurper().parseText(xml)
    return node
}

以上我都得到了

Scripts not permitted to use new groovy.util.XmlSlurper. Administrators can decide whether to approve or reject this signature.

现在我不想使用XmlParser或XmlSlurper,所以我想知道是否有其他方法可以在管道中解析xml文件。

anauzrmj

anauzrmj1#

最后我用powershell脚本来做这件事。

withEnv(["param=$param", "param2=$param2"]){
powershell '''
    [xml]$xmlContent = Get-Content ./path/file.xml
    foreach ($endpoint in $xmlContent.node.endpoint) {
        if($endpoint.name -eq "$env:someVar"){
            $lines = get-content ./path/file.xml
            $lines | foreach-object {$_ -replace $endpoint.attribute, "replace content"} | set-content ./path/file.xml
        }
    }
'''}

相关问题