我需要能够在带有时区的XML文档中格式化日期,然后用格式化的日期替换原始日期并重写XML。
下面是示例XML,其中我可以有多个日期,例如<packing>
:
import static java.util.Calendar.*
import groovy.xml.XmlUtil
import groovy.util.XmlSlurper
def timeZone = TimeZone.getTimeZone('America/Cancun')
def xml = ''' <Body>
<unit_measure>2010-12-25T12:30:45Z</unit_measure>
<product>
<number_packages>10</number_packages>
<packing>2010-12-25T12:30:45Z</packing>
<product_information>157230 TIRES RUBBER PNEUMATIC NOI</product_information>
<weight>251.0</weight>
</product>
<product>
<number_packages>10</number_packages>
<packing>2010-12-25T14:30:45Z</packing>
<product_information>157230 TIRES RUBBER PNEUMATIC NOI</product_information>
<weight>251.0</weight>
</product>
</Body>'''
def a = 'blabla'
def result = new XmlSlurper().parseText(xml)
result.'**'.findAll { it.name().startsWith 'packing' }.each { node -> node.replaceBody(node.value+"1") }
groovy.xml.XmlUtil.serialize result
我可以找到我所有的packing
值,但是如何用新值替换每个值?