gpt4 book ai didi

xml - Groovy使用XPath在xml中重复引用节点值(插值滥用?)

转载 作者:行者123 更新时间:2023-12-03 16:27:34 24 4
gpt4 key购买 nike

设置:SOAP UI 5.2.0。,Groovy步骤生成XML。
我们需要阅读一个CSV,其中包含类似XPath的节点位置以及要放置以采样XML的新值。
以下答案中的代码最新版本完全符合我们的目标:Groovy replace node values in xml with xpath
只有一个问题:
我们的XML包含重复元素,并且无法使用,因为它会误解Body.GetWeather[1].CityName中的“ [1]”

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


理想情况下,我们还需要使用 Body.GetWeather[CountryName="Africa"].CityName之类的东西。我也尝试使用 XMLParser并尝试了语法(请参见下文)。我是Groovy的新手,我可能会丢失一些东西。
因此,请告诉我是否需要以不同的方式解决问题。

下面是第三个示例中描述的实际问题:

// reading XML
def myInputXML = '''
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<web:GetWeather xmlns:web="http://www.webserviceX.NET">
<web:CityName>Cairo</web:CityName>
<web:CountryName>Africa</web:CountryName>
</web:GetWeather>
<web:GetWeather xmlns:web="http://www.webserviceX.NET">
<web:CityName>Heidelberg</web:CityName>
<web:CountryName>Germany</web:CountryName>
</web:GetWeather>
<web:GetWeather xmlns:web="http://www.webserviceX.NET">
<web:CityName>Strasbourg</web:CityName>
<web:CountryName>France</web:CountryName>
</web:GetWeather>
</soapenv:Body>
</soapenv:Envelope>
'''
def xml = new XmlSlurper().parseText( myInputXML )
// Example 1 //
def GetAllCities = xml.Body.GetWeather.CityName
log.info ("Example 1: "+GetAllCities.text()) // references all 3 CityName nodes, prints out - CairoHeidelbergStrasbourg
// Example 2 //
def Get2ndCity = xml.Body.GetWeather[1].CityName
log.info ("Example 2: "+Get2ndCity.text()) // references 2nd node, prints out - Heidelberg
// Example 3 //
def tmpNode1 = "Body"
def tmpNode2 = "GetWeather[0]"
// This problem is with interpolation of GetWeather[0]. tmpNode2 = "GetWeather" would work as Example 1
def tmpNode3 = "CityName"
def GetFirstCity = xml."${tmpNode1}"."${tmpNode2}"."${tmpNode3}"
log.info ("Example 3: "+GetFirstCity.text()) // prints "" - WHY?
log.info ("Interpolation of tmpNodes 1, 2, 3:")
log.info ("${tmpNode1}") // prints Body
log.info ("${tmpNode2}") // prints GetWeather[0]
log.info ("${tmpNode3}") // prints CityName


附言如果我的示例与实际问题无关,我们深表歉意,但我认为它们会有所帮助,但目标是改进上述stackoverflow答案以支持重复元素。

最佳答案

如果您只想修复脚本,请进行以下更改以获取所需的数据。



// Example 3 //
def tmpNode1 = "Body"
def tmpNode2 = "GetWeather[0]"
// This problem is with interpolation of GetWeather[0]. tmpNode2 = "GetWeather" would work as Example 1
def tmpNode3 = "CityName"
def GetFirstCity = xml."${tmpNode1}"."${tmpNode2}"."${tmpNode3}"
log.info ("Example 3: "+GetFirstCity.text()) // prints "" -




// Example 3 //
def tmpNode1 = "Body"
//removed index from here
def tmpNode2 = "GetWeather"
def tmpNode3 = "CityName"
//Added index here in below
def GetFirstCity = xml."${tmpNode1}"."${tmpNode2}"[0]."${tmpNode3}"
log.info ("Example 3: "+GetFirstCity.text())


优雅的方法:

但是,这是我从 csv文件生成请求的方式。它不涉及任何xml模板,而是使用 StreamingMarkupBuilder构建整个请求xml。因为这是一种优雅而灵活的方式。另外,csv可读性很强,因为它仅包含数据,而不包含您提到的任何 xpath's

下面的脚本使用了这个非常好的库 groovycsv(取决于opencsv),请遵循其自述文件。


下载jar文件。
将它们复制到SOAPUI_HOME / bin / ext目录下。
重新启动soapui。


这是基于csv文件构建请求xml的脚本:

import groovy.xml.*
import static com.xlson.groovycsv.CsvParser.parseCsv
//closure which builds the request based on the data provided
def requestBuilder = { csvData ->
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def soapRequest = builder.bind {
mkp.xmlDeclaration()
namespaces << [soap: 'http://schemas.xmlsoap.org/soap/envelope/',
web : 'http://www.webserviceX.NET']
soap.Envelope {
soap.Header{}
soap.Body {
//loop thru the rows
csvData.each { row ->
//create GetWeather element for each row
web.GetWeather{
web.CityName(row.CityName)
web.CountryName(row.CountryName)
}
}
}
}
}
}
//Used fixed csv data. But you can replace it with reading from file too
def csv = '''CityName,CountryName
Cairo,Africa
Heidelberg,Germany
Strasbourg,France'''
/**
//use this to read from file and remove above statement
def csv = new File('/absolute/csv/file/path').text
**/
//parse the csv using groovy csv library
def data = parseCsv(csv)
//call the above closure get the request and serialize it to string
def request = XmlUtil.serialize(requestBuilder(data))
log.info request


如果使用 print request而不是 log.info,它将显示漂亮的xml(您需要在命令行SOAPUI_HOME / bin / soapui.bat中启动soapui)
enter image description here

关于xml - Groovy使用XPath在xml中重复引用节点值(插值滥用?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37956927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com