What is the common practice in handling the response of a callout?
the response I get is a String datatype but it's value has xml elements within in them.
I was thinking of manipulating the contents manually and putting it in a map but I am not quite certain if this is the common approach.
SOAP Response:
Is putting this in a map the best approach?
Or is it suppose to be set in an object? (a DTO?) If so, how?
Method returning a String
public class GlobalWeatherSoap {
public String endpoint_x = 'http://www.webservicex.net/globalweather.asmx';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://www.webserviceX.NET', 'GlobalWeatherWSDL'};
public String GetCitiesByCountry(String CountryName) {
GlobalWeatherWSDL.GetCitiesByCountry_element request_x = new GlobalWeatherWSDL.GetCitiesByCountry_element();
request_x.CountryName = CountryName;
GlobalWeatherWSDL.GetCitiesByCountryResponse_element response_x;
Map<String, GlobalWeatherWSDL.GetCitiesByCountryResponse_element> response_map_x = new Map<String, GlobalWeatherWSDL.GetCitiesByCountryResponse_element>();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.webserviceX.NET/GetCitiesByCountry',
'http://www.webserviceX.NET',
'GetCitiesByCountry',
'http://www.webserviceX.NET',
'GetCitiesByCountryResponse',
'GlobalWeatherWSDL.GetCitiesByCountryResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.GetCitiesByCountryResult;
}
for reference this is the wsdl that I have imported
http://www.webservicex.net/globalweather.asmx?WSDL — I removed all port related elements that are not named "GlobalWeatherSoap" this allowed me to import and generate the apex wsdl
Best Answer
If you use wsdl2apex, you'll get native DTO-style functionality in your Apex Code. From a developer standpoint, all you write is something like this:
To invoke wsdl2apex, go to Setup > Develop > Apex Classes > Import WSDL.
Not all WSDL files can be imported, though, so you may not be able to directly use this approach. If that's the case, You'll want to use Dom.Document to load your XML file, then retrieve the elements you want using the various Dom.XmlNode functions.
The Dom.Document/Dom.XmlNode interface lets you retrieve various elements quickly and efficiently, or iterate through them in a controlled manner without the need for manual parsing.