Search code examples
objective-cxmlparsingspecial-charactersnsxmlparser

NSXMLParser can not parse special characters (german & french)


I am working on an App, which makes a search on a private server and shows the results to the user. The problem is NSXLParser can not parse the special german and french characters. For example: it should be:(Geschäftsführer) -> what i get is: (äftsführer)

How can i fix this ?

here is my code:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"results"])
    {
        currentJob = [SearchResult alloc];
    }

}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{


    if ([elementname isEqualToString:@"jobTitle"])
    {
        currentJob.jobTitle = currentNodeContent;
    }

    if ([elementname isEqualToString:@"location"])
    {
        currentJob.shortAddress = currentNodeContent;
    }

    if ([elementname isEqualToString:@"companyName"])
    {
        currentJob.employer = currentNodeContent;
    }

    if ([elementname isEqualToString:@"results"])
    {
        [self.jobs addObject:currentJob];
        currentJob = nil;
        currentNodeContent = nil;
    }
}

Any help would be much appreciated... Thanks in advance


Solution

  • Your foundCharacters method should append the string to a NSMutableString object, because it can be called multiple times for a single value. In didStartElement, initialize a NSMutableString object (let's call it elementContentString), then do this:

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)chars
    {
        [self.elementContentString appendString:chars];
    }
    

    And in your didEndElement you can get the content as a string. Note that you should make a non-mutable copy of it (so you don't overwrite it with the next element), using [NSString stringWithString:self.elementContentString].