【翻译】用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:

我是Groovy新手。我要完成一项Jenkins配置的任务。请教我解析xml。为了简述问题,下面只是截取了一部分Jenkins的配置文件config.xml:

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.

目标只是改变Pen的颜色。

I'm trying:

我尝试:

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.

看起来简单我却被搞得一头雾水了。求救。


Almost there...

几乎要实现了。。。

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

转载于:https://my.oschina.net/u/553266/blog/391843

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值