【翻譯】用Groovy來更新XML

I'm newbie in Groovy and have to accomplish a task for some Jenkins configuration. Please, help me with xml parsing. Just in order to simplify the problem(originally it's a huge Jenkins config.xml file), let's take:html

我是Groovy新手。我要完成一項Jenkins配置的任務。請教我解析xml。爲了簡述問題,下面只是截取了一部分Jenkins的配置文件config.xml:node

def input = '''
              <shopping>
                  <category>
                      <item>Pen</item>
                      <color>Red</color>
                  </category>
                  <category>
                      <item>Pencil</item>
                      <color>Black</color>
                  </category>
                  <category>
                      <item>Paper</item>
                      <color>White</color>
                  </category>
              </shopping>
    '''

The target is to change color for Pen only.spa

目標只是改變Pen的顏色。code

I'm trying:orm

我嘗試:xml

def root = new XmlParser().parseText(input)def supplies = root.category.find{ it.text() == 'Pen' } supplies.parent().color.value() = 'Changed'

Looks so simple but I'm totally lost :( Appreciate any help.htm

看起來簡單我卻被搞得一頭霧水了。求救。ci


Almost there...get

幾乎要實現了。。。input

def root = new XmlParser().parseText(input)
def supplies = root.category.find{ it.item.text() == 'Pen' }
supplies.color[0].value = 'Changed'

The thing to note is that color is a Node List whose first node is a text node

要注意的是color是一個Node列表,而其第一個節點是文本節點


....Or use XmlSlurper to simplify usage of color[0] and text().

或者使用 XmlSlurper 去簡化 color[0] 和 text() 。

def root = new XmlSlurper().parseText(input)
def supplies = root.category.find{ it.item == 'Pen' }
supplies.color = 'Changed'

完整代碼一:

import groovy.xml.XmlUtil
def input = '''
              <shopping>
                  <category>
                      <item>Pen</item>
                      <color>Red</color>
                  </category>
                  <category>
                      <item>Pencil</item>
                      <color>Black</color>
                  </category>
                  <category>
                      <item>Paper</item>
                      <color>White</color>
                  </category>
              </shopping>
    '''
    
def root = new XmlParser().parseText(input)
def supplies = root.category.find{ it.item.text() == 'Pen' }
supplies.color[0].value = 'Changed'


//println root.toString()
println XmlUtil.serialize(root)

完整代碼二:

import groovy.xml.XmlUtil
def input = '''
              <shopping>
                  <category>
                      <item>Pen</item>
                      <color>Red</color>
                  </category>
                  <category>
                      <item>Pencil</item>
                      <color>Black</color>
                  </category>
                  <category>
                      <item>Paper</item>
                      <color>White</color>
                  </category>
              </shopping>
    '''
    
def root = new XmlSlurper().parseText(input)
def supplies = root.category.find{ it.item == 'Pen' }
supplies.color = 'Changed'


//println root.toString()
println XmlUtil.serialize(root)


來自: http://wenda.baba.io/questions/3993279/groovy-update-xml.html

相關文章
相關標籤/搜索