我遇到了NSXMLParser的一个特殊问题。
出于某种原因,它会删除所有挪威字符æ,ø和å前面的所有字符。
然而,问题似乎与所有非a-z字符相同。(所有外国字符)
示例:
现实:Mål 输出:ål
现实:Le chantdessirènes 输出:ènes
下面是我从日志中打印出来的一个例子:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
日志:
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
我已经检查了SO的答案并发现了几个类似的帖子,但没有任何方法可以解决这个问题。
有人能解释一下这个问题吗? :)非常感谢任何帮助!
答案 0 :(得分:9)
你的解析器:foundCharacters:方法不能正常工作。
这来自NSXMLParserDelegate Protocol Reference
解析器对象可以向委托发送几个解析器: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;
}