iosencodingnsxmlparsernsdatagoogle-weather-api

google weather arabic xml encoding in ios


I tried to retrieve XML Google weather API from this URL:

http://www.google.com/ig/api?weather=,,,24467280,39606640&hl=ar

It is in Arabic language.

I used: NSURLConnection to retrieve this data and then parsed it by NSXMLParser.

The received data is not correctly encoded.I tried to put it in XML file after imported it and I found data like this:

<?xml version="1.0"?><xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="21429730"/><longitude_e6 data="39828370"/><forecast_date data="2012-03-14"/><current_date_time data="2012-03-14 17:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="’«›Ú"/><temp_f data="97"/><temp_c data="36"/><humidity data="«·—ÿÊ»…: &#x662;&#x662;&#x66A;"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="«·—Ì«Õ: Éʻ »”—⁄… 8 fl„/”«⁄…"/></current_conditions><forecast_conditions><day_of_week data="«·√—»⁄«¡"/><low data="23"/><high data="37"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·Œ„Ì”"/><low data="20"/><high data="35"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·Ã„⁄…"/><low data="18"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions><forecast_conditions><day_of_week data="«·”» "/><low data="19"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="„‘„” ›Ì √€·» «·√Êfi« "/></forecast_conditions></weather></xml_api_reply>

Of course, I got error from NSXMLParser:

Error Domain=NSXMLParserErrorDomain Code=9 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 9.)"

That means the xml data is corrupted.

If I change the link from Google to be:

http://www.google.com/ig/api?weather=,,,21429730,39828370&hl=en

(notice english language now), all things works correctly..

I tried to encode the retrieved Arabic XML data by:

NSString * encodedString = [[NSString alloc] initWithBytes:recievedData.bytes length:recievedData.length encoding:NSStringEncodingConversionAllowLossy];

and then I putted the content in the file by:

[[NSFileManager defaultManager] createFileAtPath:filePath contents:[encodedString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

the content now is better but it is still wrong, the content now :

<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="21429730"/><longitude_e6 data="39828370"/><forecast_date data="2012-03-14"/><current_date_time data="2012-03-14 17:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="ÕÇÝò"/><temp_f data="97"/><temp_c data="36"/><humidity data="ÇáÑØæÈÉ: &#x662;&#x662;&#x66A;"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="ÇáÑíÇÍ: ÌäæÈ ÈÓÑÚÉ 8 ßã/ÓÇÚÉ"/></current_conditions><forecast_conditions><day_of_week data="ÇáÃÑÈÚÇÁ"/><low data="23"/><high data="37"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÎãíÓ"/><low data="20"/><high data="35"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÌãÚÉ"/><low data="18"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions><forecast_conditions><day_of_week data="ÇáÓÈÊ"/><low data="19"/><high data="33"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="ãÔãÓ Ýí ÃÛáÈ ÇáÃæÞÇÊ"/></forecast_conditions></weather></xml_api_reply>

The NSXMLParser doesn't give me error now but the text results in not readable.

Sorry for this large question.

Can any one help me?


Solution

  • Add [oe=char_set] parameter to google url as follow:

    `http://www.google.com/ig/api?weather=Cairo,Egypt&hl=ar&oe=utf-8`