Extracting CDATA from Soap answer in groovy

I am working on the http://webserviceX.NET Web Service Sample as it keeps returning a response in CDATA. I'm trying to print the response of my request in groovy, but it returns null. I did this as my Groovy coding practice. Please bear with me as I just started learning the language and everything about SOAP.

here is my code:

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.8.0' )
import wslite.soap.*
class GlobalWeather {
    def spUrl = ('http://www.webservicex.net/globalweather.asmx')
    def client = new SOAPClient(spUrl)

    def getCitiesByCountry(String country) {
        def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry"){
            body {
                GetCitiesByCountry('xmlns': 'http://www.webserviceX.NET') {
                    CountryName(country)
                }
            }
        }
        def retCountry = response.CitiesByCountryResponse.CitiesByCountryResult
        return retCountry
    }

    def getWeather(def city, def country){
        def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetWeather"){
            body{
                GetWeather('xmlns': 'http://www.webserviceX.NET'){
                    CityName(city)
                    CountryName(country)
                }
            }
        }
        def retCountryCity = response.WeatherResponse.WeatherResult
        return retCountryCity
    }

    static void main(String[] args){
        def gWeather = new GlobalWeather()

        println gWeather.getCitiesByCountry('UK')
        println gWeather.getWeather('Kyiv', 'Ukraine')
    }
}

      

+3


source to share


1 answer


You used the wrong variable names when processing the response:

def retCountry = response.CitiesByCountryResponse.CitiesByCountryResult
return retCountry

      

instead:

return response.GetCitiesByCountryResponse.GetCitiesByCountryResult

      

and

def retCountryCity = response.WeatherResponse.WeatherResult
return retCountryCity

      



instead:

return response.GetWeatherResponse.GetWeatherResult

      

Omitting Get

won't work here because it's not a variable name (no getter) but a node name.

You can find the corrected script below:

@Grab(group = 'com.github.groovy-wslite', module = 'groovy-wslite', version = '0.8.0')
import wslite.soap.*

class GlobalWeather {
    def spUrl = ('http://www.webservicex.net/globalweather.asmx')
    def client = new SOAPClient(spUrl)

    def getCitiesByCountry(String country) {
        def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry") {
            body {
                GetCitiesByCountry('xmlns': 'http://www.webserviceX.NET') {
                    CountryName(country)
                }
            }
        }
        return response.GetCitiesByCountryResponse.GetCitiesByCountryResult
    }

    def getWeather(def city, def country) {
        def response = client.send(SOAPAction: "http://www.webserviceX.NET/GetWeather") {
            body {
                GetWeather('xmlns': 'http://www.webserviceX.NET') {
                    CityName(city)
                    CountryName(country)
                }
            }
        }
        return response.GetWeatherResponse.GetWeatherResult
    }
}

def gWeather = new GlobalWeather()

println gWeather.getCitiesByCountry('UK')
println gWeather.getWeather('Kyiv', 'Ukraine')

      

PS Upvote to prepare a working example! This is how the question should be asked here.

+4


source







All Articles