0

I am facing problem in converting the Spanish text that is fetched from the service in

correct format. Server side they are encoding with ISO-8859-1. It is a xml service. In my

iOS7 app, I am using TBXml parser to parse the data. The code is:

NSString *XMLString = [[NSString alloc] initWithContentsOfURL:urlString encoding:NSISOLatin1StringEncoding error:nil];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

I am parsing this data, but the when there are Spanish characters like "BEBÉS Y" the my

string will be "BEB…S Y" . And "øPor quÈ albergamos a alborotadores?" instead of "¿Por qué albergamos a alborotadores?" . Please help

Larme
  • 24,190
  • 6
  • 51
  • 81
Raju goud Bingi
  • 160
  • 2
  • 13
  • Your code looks fine. Are you sure it's actually ISO-8859-1 encoding? You can switch to the method `[NSString -initWithContentsOfFile:usedEncoding:error:]` to have it try to automatically detect the encoding (it will report back to you what it used.) – Aaron Brager Feb 25 '14 at 16:04
  • @AaronBrager It would be better to download binary and let the XML parser decide on the encoding. – Sulthan Feb 25 '14 at 22:13

2 Answers2

1

You should download the XML data as binary (NSData) and let the parser handle the encoding.

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
TBXML *tbxml = [TBXML tbxmlWithXMLData:data error:&error];

Note the XML should have the content encoding as the first line so there is no need to specify an encoding in code.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

Try with NSUTF8StringEncoding

NSString *XMLString = [[NSString alloc] initWithContentsOfURL:urlString encoding:NSUTF8StringEncoding error:nil];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

UPDATE:

NSData *dataContent = [[NSData dataWithContentsOfURL:urlString];
NSString *XMLString = [[NSString alloc] initWithData:dataContent encoding:NSISOLatin1StringEncoding];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

UPDATE 2: Try with data initialization

NSData *dataContent = [[NSData dataWithContentsOfURL:urlString];

TBXML *tbxml = [[TBXML alloc] initWithXMLData:XMLString];
Fran Martin
  • 2,369
  • 22
  • 19