Setup: SOAP UI 5.2.0., Groovy step to generate XMLs.
We need to read a CSV which contains XPath-like nodes locations and new values to be placed to sample XML.
The last version of the code in the following answer fits our goal perfectly fine: Groovy replace node values in xml with xpath
Only one problem:
our XML contains repeating elements and using is not possible as it misinterprets "[1]" in Body.GetWeather[1].CityName
def node = xml
key.split("\\.").each {
node = node."${it}"
}
Ideally, we would also need to use something like Body.GetWeather[CountryName="Africa"].CityName
.
I tried using XMLParser
also and experimented with syntax (see below). I am new to Groovy and I might be missing something about it.
So, let me know if I need to approach the problem differently.
Below is the actual problem described in 3rd example:
// 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
P.S. Apologies in case if my examples are irrelevant for the actual problem, I thought they are somewhat helpful, but the goal is to improve the mentioned stackoverflow answer to support repeating elements.