Groovy脚本 - 在索引0处将节点添加为子节点

时间:2017-06-01 18:23:51

标签: xml jenkins groovy jenkins-pipeline

我有问题能够将节点添加到节点子节点的0索引。

我能够将一个节点追加到最后,但无法弄清楚如何在前面找到一个节点。

我的代码目前看起来像这样。

def setupProjectXML(workspace, environment, client) {
    def projectFile = new XmlParser().parse(new File("${workspace}/jenkinsProject.xml"))

    projectFile.appendNode("set", [name : "m4"])
    projectFile.appendNode("set", [name : "universalM4"])
    projectFile.appendNode("set", [name : "restoreState"])
    projectFile.appendNode("set", [name : "staging"])

    println(projectFile.toString())
}

我尝试了一些不同的方式使用New Node()并为NodeList添加,但是这会在我使用Node构造函数的时候崩溃脚本。

此错误是

an exception which occurred:
in field collector
in field abnormal
in field outcome
in field body
in field step
in field thread
in field this$0
in field returnAddress
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field caller
in field parent
in field parent
in field capture
in field def
in field closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@2b1645bc
Caused: java.io.NotSerializableException: org.codehaus.groovy.control.ErrorCollector

2 个答案:

答案 0 :(得分:2)

def xml = new XmlParser().parseText('''<a><b c="d"/></a>''')
println groovy.xml.XmlUtil.serialize(xml)

xml.children().add(0, new Node(null, 'set', [x:'y'], 'text'))
println groovy.xml.XmlUtil.serialize(xml)

输出:

<?xml version="1.0" encoding="UTF-8"?><a>
  <b c="d"/>
</a>

<?xml version="1.0" encoding="UTF-8"?><a>
  <set x="y">text</set>
  <b c="d"/>
</a>

答案 1 :(得分:0)

我得到了它。

def setupProjectXML(workspace, environment, client) {
    def projectFile = new XmlParser(false,true).parse(new File("${workspace}/jenkinsProject.xml"))
    projectFile.appendNode("set", [name : "m4"])
    projectFile.appendNode("set", [name : "universalM4"])
    projectFile.appendNode("set", [name : "restoreState"])
    projectFile.appendNode("set", [name : "staging"]) projectFile.children().get(0).replaceNode(projectFile.children().get(projectFile.children().size()-1))
projectFile.children().get(1).replaceNode(projectFile.children().get(projectFile.children().size()-2))
projectFile.children().get(2).replaceNode(projectFile.children().get(projectFile.children().size()-3))
projectFile.children().get(3).replaceNode(projectFile.children().get(projectFile.children().size()-4))

FileWriter fileWriter = new FileWriter("${workspace}/jenkinsProject.xml")
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(fileWriter))
nodePrinter.setPreserveWhitespace(true)
nodePrinter.print(projectFile)
}

这是一种丑陋的方式,我不得不在我的xml类中添加空节点以用于替换。仍然不确定为什么我不能调用新的Node()。