5

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

Zer0
  • 113
  • 10
  • can you give a link to the XML file you are parsing? – kuba Sep 26 '12 at 09:39
  • also your `foundCharacters` method is wrong - you can get more than one call to this function even for a single string value – kuba Sep 26 '12 at 09:40

1 Answers1

3

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].

kuba
  • 7,329
  • 1
  • 36
  • 41
  • Thank you very much! I used the following code and it works now. if(!elementContentString) elementContentString = [[NSMutableString alloc] initWithString:string]; else [elementContentString appendString:string]; – Zer0 Sep 26 '12 at 12:48
  • I have a ´ character that is causing my parser to stop. How do I handle that? My foundCharacters method looks just like the one @kuba suggested. – marciokoko Apr 23 '13 at 02:44