nap011
nap011

Reputation: 231

Extracting CDATA from a soap response in groovy

Im working on a sample web service at http://webserviceX.NET, somehow it keeps returning it's response within a CDATA. i am trying out to print the response of my request in groovy but it returns null. I was doing this as my practice coding in Groovy. 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')
    }
}

Upvotes: 3

Views: 879

Answers (1)

Opal
Opal

Reputation: 84786

You've used incorrect variable names when processing response:

def retCountry = response.CitiesByCountryResponse.CitiesByCountryResult
return retCountry

instead of:

return response.GetCitiesByCountryResponse.GetCitiesByCountryResult

and:

def retCountryCity = response.WeatherResponse.WeatherResult
return retCountryCity

instead of:

return response.GetWeatherResponse.GetWeatherResult

Omitting Get here will not work because this is not a name of a variable (there's no getter) but a node name.

Below You can find corrected script:

@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')

P.S. Upvote for preparing a working example! That's how question should be asked here.

Upvotes: 4

Related Questions