Groovy用xpath替换xml中的节点值

Groovy用xpath替换xml中的节点值,xml,xpath,groovy,Xml,Xpath,Groovy,我想在groovy中替换xml中的节点值。 我在hashmap中有xpath中的值,如: def param = [:] param["/Envelope/Body/GetWeather/CityName"] = "Berlin" param["/Envelope/Body/GetWeather/CountryName"] = "Germany" XML文件: <?xml version="1.0" encoding="UTF-8"?><soapenv:

我想在groovy中替换xml中的节点值。 我在hashmap中有xpath中的值,如:

 def param = [:]       
 param["/Envelope/Body/GetWeather/CityName"] = "Berlin"
 param["/Envelope/Body/GetWeather/CountryName"] = "Germany"
XML文件:

 <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
      <web:GetWeather xmlns:web="http://www.webserviceX.NET">
          <web:CityName>Test</web:CityName>
          <web:CountryName>Test</web:CountryName>
      </web:GetWeather>
  </soapenv:Body>
</soapenv:Envelope>

试验
试验
如何替换节点值?

您可以尝试使用,这可能是一种简单的方法。您可以使用节点名作为键,文本作为值来定义映射,并在Xml中更改节点。您可以使用类似的代码,如下所示:

import groovy.util.XmlSlurper
import groovy.xml.XmlUtil

def xmlString = '''<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
      <web:GetWeather xmlns:web="http://www.webserviceX.NET">
          <web:CityName>Test</web:CityName>
          <web:CountryName>Test</web:CountryName>
      </web:GetWeather>
  </soapenv:Body>
</soapenv:Envelope>'''

def param = [:]       
param["CityName"] = "Berlin"
param["CountryName"] = "Germany"

// parse the xml
def xml = new XmlSlurper().parseText(xmlString)

// for each key,value in the map
param.each { key,value ->
    // change the node value if the its name matches
    xml.'**'.findAll { if(it.name() == key) it.replaceBody value }
}

println XmlUtil.serialize(xml)
代码中的所有内容:

import groovy.util.XmlSlurper
import groovy.xml.XmlUtil

def xmlString = '''<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
      <web:GetWeather xmlns:web="http://www.webserviceX.NET">
          <web:CityName>Test</web:CityName>
          <web:CountryName>Test</web:CountryName>
      </web:GetWeather>
  </soapenv:Body>
</soapenv:Envelope>'''

def param = [:]       
// since envelope is the root node it's not necessary
param["Body.GetWeather.CityName"] = "Berlin"
param["Body.GetWeather.CountryName"] = "Germany"

def xml = new XmlSlurper().parseText(xmlString)

param.each { key,value ->
    def node = xml
    key.split("\\.").each {
      node = node."${it}"
    }
    node.replaceBody value
}

println XmlUtil.serialize(xml)
这段代码来自于此,它是使用变量解决基于路径的
问题的一种变通方法(IMO
是一种很好的变通方法:)


希望这有帮助,

嗨,这对这个例子有帮助,但我需要一些更通用的东西。例如,当xml看起来像这样时,我应该如何处理?这就是我想用的方法xpath@Peter您可以在答案中使用第二种方法,使用第二种方法,您可以将
路径添加到地图中的元素。您好,我必须尝试您的另一种解决方案,只是尝试了第一种one@Peter很好,希望对我有帮助。
:)是的,第二个解决方案对我很有效,非常感谢
import groovy.util.XmlSlurper
import groovy.xml.XmlUtil

def xmlString = '''<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
      <web:GetWeather xmlns:web="http://www.webserviceX.NET">
          <web:CityName>Test</web:CityName>
          <web:CountryName>Test</web:CountryName>
      </web:GetWeather>
  </soapenv:Body>
</soapenv:Envelope>'''

def param = [:]       
// since envelope is the root node it's not necessary
param["Body.GetWeather.CityName"] = "Berlin"
param["Body.GetWeather.CountryName"] = "Germany"

def xml = new XmlSlurper().parseText(xmlString)

param.each { key,value ->
    def node = xml
    key.split("\\.").each {
      node = node."${it}"
    }
    node.replaceBody value
}

println XmlUtil.serialize(xml)
    def node = xml
    key.split("\\.").each {
      node = node."${it}"
    }