[erlang-questions] Encoding issues with xmerl
Milton Inostroza
minostro@REDACTED
Sun Mar 22 21:56:48 CET 2015
Hi All,
I'm just trying to write a small SOAP client to consume a very single soap endpoint in Erlang. I'm able to get the response from the server which is as follows:
"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetWeatherResponse xmlns=\"http://www.webserviceX.NET\"><GetWeatherResult><?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<CurrentWeather>\r\n <Location>CHICAGO/WAUKEGAN REGIONAL, IL, United States (KUGN) 42-25N 087-52W</Location>\r\n <Time>Mar 22, 2015 - 03:55 PM EDT / 2015.03.22 1955 UTC</Time>\r\n <Wind> from the NE (050 degrees) at 9 MPH (8 KT):0</Wind>\r\n <Visibility> 10 mile(s):0</Visibility>\r\n <SkyConditions> mostly cloudy</SkyConditions>\r\n <Temperature> 30.0 F (-1.1 C)</Temperature>\r\n <Wind>Windchill: 21 F (-6 C):1</Wind>\r\n <DewPoint> 14.0 F (-10.0 C)</DewPoint>\r\n <RelativeHumidity> 50%</RelativeHumidity>\r\n <Pressure> 30.28 in. Hg (1025 hPa)</Pressure>\r\n <Status>Success</Status>\r\n</CurrentWeather></GetWeatherResult></GetWeatherResponse></soap:Body></soap:Envelope>"
I'm able to get a proper Erlang tuple by calling xmerl_scan:string(Response). I get problems when trying to convert the Body part of the response which is another xml document. The error that I get is the following:
{XmlData, _} = xmerl_scan:string(lists:append(Data)).
3899- fatal: expected_element_start_tag
** exception exit: {fatal,{expected_element_start_tag,{file,file_name_unknown},
{line,2},
{col,2}}}
in function xmerl_scan:fatal/2 (xmerl_scan.erl, line 4102)
in call from xmerl_scan:scan_document/2 (xmerl_scan.erl, line 567)
in call from xmerl_scan:string/2 (xmerl_scan.erl, line 286)
As a workaround I'm calling the xmerl_scan:string function passing a second parameter: [{encoding, latin1}] This allows xmerl to successfully convert the string into an Erlang tuple, which makes me believe that there is a encoding problem somewhere.
My script looks like this:
application:start(inets).
SoapBody = "<soap:Body><GetWeather xmlns=\"http://www.webserviceX.NET\"><CityName>Chicago</CityName><CountryName>United States</CountryName></GetWeather></soap:Body>".
SoapEnvelope = lists:append([
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">",
SoapBody,
"</soap:Envelope>"]
).
{ok, {{_, _, _}, _, ResponseBody}} = httpc:request(post, {"http://www.webservicex.net/globalweather.asmx", [], "text/xml", SoapEnvelope}, [], []).
{Xml, _} = xmerl_scan:string(ResponseBody).
{_, _, [{_, _, [{_, _, [{_, [], Data}]}]}]} = xmerl_lib:simplify_element(Xml).
{XmlData, _} = xmerl_scan:string(lists:append(Data), [{encoding, latin1}]).
FinalResult = xmerl_lib:simplify_element(XmlData).
I would really like to avoid calling xmerl_scan:string with the second argument, but I cannot figure how to do that. Any ideas?
Thanks,
--Milton
More information about the erlang-questions
mailing list