NSXMLParser divides strings containing foreign(uni

2019-06-01 03:01发布

问题:

I have ran into a peculiar problem with NSXMLParser.

For some reason it cuts out all the characters in front of all the norwegian characters æ, ø and å.

However, the problem seems to be the same with all non a-z characters.(All foreign characters)

Examples:

Reality: Mål Output: ål

Reality: Le chant des sirènes Output: ènes

Heres an example from the log where I have printed out the string from:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

Log:

2012-02-22 14:00:01.647 VotePlayClient[2776:207] found characters: Le chant des sir
2012-02-22 14:00:01.647 VotePlayClient[2776:207] found characters: ènes

You can clearly see that it jumps to a new line whenever it encounters a foreign letter.

I believe that I have to figure out how to append the string or something to that effect.

Here are the NSXMLParser files:

SearchXMLParser.h

#import <Foundation/Foundation.h>
#import "Search.h"

@interface SearchXMLParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentNodeContent;
NSMutableArray *searchhits;
NSMutableArray *trackhits;

NSXMLParser *parser;
Search *currentSearch;  
}

@property (readonly, retain) NSMutableArray *searchhits;
@property (readonly, retain) NSMutableArray *trackhits;

-(id) loadXMLByURL:(NSString *)urlString;

@end

SearchXMLParser.m

#import "SearchXMLParser.h"
#import "Search.h"

@implementation SearchXMLParser

@synthesize searchhits, trackhits;

-(id) loadXMLByURL:(NSString *)urlString
{
    searchhits          = [[NSMutableArray alloc] init];
trackhits           = [[NSMutableArray alloc] init];
NSURL *url      = [NSURL URLWithString:urlString];
NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
parser          = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;    
}

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

if ([elementname isEqualToString:@"track"]) 
{
    currentSearch.trackurl = [attributeDict objectForKey:@"href"];
}
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{   
if ([elementname isEqualToString:@"name"]) 
{
    [trackhits addObject:currentNodeContent];

}

if ([elementname isEqualToString:@"track"]) 
{
    currentSearch.track = [trackhits objectAtIndex:0];
    currentSearch.artist = [trackhits objectAtIndex:1];
    currentSearch.album = [trackhits objectAtIndex:2];
    [trackhits removeAllObjects];

    [searchhits addObject:currentSearch];
    [currentSearch release];
    currentSearch = nil;
    [currentNodeContent release];
    currentNodeContent = nil;
}
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"found characters: %@", string);
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void) dealloc
{
[parser release];
[super dealloc];
}

@end

I have already checked SO for answers and found a couple of similar posts, but nothing that gave a clear solution to this problem.

Can anyone shed some light on this problem? :) Any help is much appreciated!

回答1:

your parser:foundCharacters: method does not work as it should.

This is from the NSXMLParserDelegate Protocol Reference

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.

you could try something like this (ARC):

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"found characters: %@", string);
    if (!currentNodeContent) {
        currentNodeContent = [[NSMutableString alloc] init];
    }
    [currentNodeContent appendString:string];
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{   
    // your code here

    // when you are done with the string:
    currentNodeContent = nil;
}