NSXMLParser 分割包含外来(unicode)字符的字符串

发布于 2025-01-08 09:44:55 字数 3117 浏览 0 评论 0原文

我在使用 NSXMLParser 时遇到了一个特殊问题。

由于某种原因,它删除了所有挪威语字符 æ、ø 和 å 前面的所有字符。

但是,问题似乎与所有非 az 字符相同。(所有外来字符)

示例:

Reality: Mål 输出:

现实:Le chant des Sirènes 输出: ènes

这是日志中的一个示例,我在其中打印出了以下字符串:

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

您可以清楚地看到,每当遇到外文字母时,它都会跳到新行。

我相信我必须弄清楚如何附加字符串或其他东西来达到这种效果。

以下是 NSXMLParser 文件:

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-01-15 09:44:55

您的 parser:foundCharacters: 方法无法正常工作。

这是来自 NSXMLParserDelegate 协议参考

解析器对象可以向委托发送多个 parser:foundCharacters: 消息来报告元素的字符。由于字符串可能只是当前元素总字符内容的一部分,您应该将其追加到当前的字符累积中,直到元素发生变化

你可以尝试这样的事情(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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文