开发者

Difficulty parsing response containing characters like '\u00'

开发者 https://www.devze.com 2023-03-28 06:33 出处:网络
I am using NSXML Parser to do parsing in my iPhone app. Now everything works fine except when data comes in French language.

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];
//  }
}


- (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];


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号