2017-06-01 85 views
1

我有問題能夠將節點添加到節點子節點的0索引。Groovy腳本 - 在索引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()) 
} 

我曾嘗試使用新的節點()幾個不同的方式,並添加了一個節點列表,但這個崩潰的腳本,只要我使用節點構造。

此錯誤是

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 [email protected] 
Caused: java.io.NotSerializableException: org.codehaus.groovy.control.ErrorCollector 

回答

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類中添加空節點用於替換。仍然不知道爲什麼我不能調用新的節點()。

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> 
+0

這個唯一的問題是隻要我嘗試使用節點的構造函數,我得到一個錯誤。我通過Jenkins調用這個函數,這可能是爲什麼它不起作用。 發生的異常: 在字段收集器 在字段異常.... –