xml - Groovy 用 xpath 替换 xml 中的节点值

标签 xml xpath groovy

我想用 groovy 替换 xml 中的节点值。 我在散列图中的 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>

如何替换节点值?

最佳答案

您可以尝试使用 XmlSlurper相反,这可能是一种简单的方法。您可以使用节点名称作为键来定义您的 map ,并将文本作为值迭代它以更改 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)

另一种可能的解决方案

相反,如果您想使用完整路径而不仅仅是节点名称来更改其值(更健壮),您可以使用 . 符号来定义您的 XPath / 表示法并避免使用根节点名称(在您的情况下为 Envelope),因为在解析的 xml 对象中它已经存在。所以改变你的 XPath 你可以有这样的东西:

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

全部在代码中:

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}"
    }

这个片段来自这个answercomment这是解决 . 基于使用变量的路径的解决方法(一个很好的解决方法 IMO :))

希望对您有所帮助,

关于xml - Groovy 用 xpath 替换 xml 中的节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30369986/

相关文章:

xml - XML 文档可以同时遵循 DTD 和 XML Schema 吗?

xml - 如何在同一父标签下重用本地定义的元素

xml - 使用XPath在父节点中获取文本

php - 使用 PHP SimpleXML 和 XPath 解析深度 XML

grails - 如何根据 Controller 操作强制域字段?

validation - Grails 重复错误消息

java - 如何使我的 Java JSONArray 嵌套并添加字段?

sql-server - 动态 XML 到存储过程插入

java - 我们可以在 xml 中为 android 背景制作多色渐变吗?

java - 如何使用 Java 和 XPath 列出节点文本和属性?