2

I am using NSXML Parser to do parsing in my iPhone app. Now everything works fine except when data comes in French language.

For example, data from server comes as Ch\u00e9rie FM.

Now under the string argument of foundCharacters method, I only get string as 'Ch' rest of the characters don't come up. So finally my string is truncated only to 'Ch' intead of the whole Cherie fm

What could be done?

Code:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict
{   
    if (appDelegate.objPlayer.fromFlickrorRecommend == TRUE)
    {
        if([elementName isEqualToString:@"outline"] && [[attributeDict valueForKey:@"text"] isEqualToString:@"You may also like"])
        {
            flagCheck = 1;
        }
        else if ([elementName isEqualToString:@"outline"] && [[attributeDict valueForKey:@"text"] isEqualToString:@"Genres"]) 
        {
            flagCheck = 0;
        }
        if (flagCheck == 1 && [elementName isEqualToString:@"outline"])
        {
            if([[attributeDict valueForKey:@"type"] isEqualToString:@"audio"])
            {
                [appDelegate.objPlayer.recommendDataArray addObject:attributeDict];

            }
        }
    }
    else
    {
        if ([elementName isEqualToString:@"location"])
        {
            flagCheck = 2;
        }
        else if ([elementName isEqualToString:@"url"])
        {   
            flagCheck = 3;
        }
        else if ([elementName isEqualToString:@"name"])
        {   
            flagCheck = 4;
        }
    }   
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
    if (flagCheck == 2)
    {
        flagCheck = -1;
        appDelegate.objPlayer.flickrCity = string;
    }
    else if(flagCheck == 3)
    {
        flagCheck = -1;
        appDelegate.objPlayer.stationURL = string;
    }
    else if(flagCheck == 4)
    {
        flagCheck = -1;
        appDelegate.playStationName = string;
    }
    //else if(flagCheck == 0) // change
//  {
//      appDelegate.playStationName = string;
//  }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{   
    //if (flagCheck == 1 && [elementName isEqualToString:@"outline"])
//  {
//      [appDelegate.objPlayer.recommendDataArray addObject:dataDictionary];
//      dataDictionary = nil;
//      [dataDictionary release];
//  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • Is `-parser:foundCharacters:` being called more than once? – jtbandes Aug 16 '11 at 16:49
  • yeah..but it doesn't bring in the other characters of the string `Ch\u00e9rie FM.` – Parth Bhatt Aug 16 '11 at 16:55
  • It only shows `Ch` under foundCharacters method and the other characters `\u00e9rie fm` dont come. Though foundCharacters method is called multiple times. – Parth Bhatt Aug 16 '11 at 17:00
  • What do you get the second time foundCharacters is called? – jtbandes Aug 16 '11 at 17:17
  • How are you getting the XML string that is being parsed? Have you verified that the source string is being properly assembled in memory and that no encoding problems are introducing data corruption? – Tim Dean Aug 16 '11 at 17:46

2 Answers2

2

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string may be called multiple times so you need to accumulate the characters found into an NSMutableString. There is an example of how to implement this in the Event-Driven XML Programming Guide.

The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

Now \u00e9 is UTF-16 for é so the data must be properly encoded to parse past \u00. So if your data was initially a string you can get the data from it like this.

NSString *text = @"<node>Ch\u00e9rie</node>";
//Important or the parser will stop after Ch
NSData *utf16encode = [text dataUsingEncoding:NSUTF16StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:utf16encode];
Joe
  • 56,979
  • 9
  • 128
  • 135
  • Thanks for the response. I will try that out soon and get back to you. :) – Parth Bhatt Aug 16 '11 at 19:58
  • I found out that NSXMLParser takes UTF-8 as default encoding. Also the above solution with `NSData *utf16encode = [text dataUsingEncoding:NSUTF16StringEncoding];` and `NSXMLParser *parser = [[NSXMLParser alloc] initWithData:utf16encode];` doesn't work for me. Still gives the same problem. So what could be done? – Parth Bhatt Aug 17 '11 at 05:31
0

Got the Answer:

This link helped while I was going through stackoverflow for the questions similar to my problem.

Why does arrays handle strings containing swedish ÅÄÖ characters by using two or more indexes?

Hope this helps all who are looking out for a solution. :)

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216