所以我正在制作一个应用程序,其中一部分在表格视图中显示用户的推文。但是,某些推文会损坏某些内容,例如它仅显示单个字符,例如(“)或表情符号字符。例如,如果推文说:
RT @jakemillermusic: Everyone upload your pics that you took today during the ustream and caption it "follow @jakemillermusic #jakemiller"
当与 NSLog 一起显示时,它会打印:
2013-04-03 00:34:30.476 ParsingXMLTutorial[3308:c07] RT @jakemillermusic:每个人都上传你今天在 ustream 中拍摄的照片并添加标题
2013-04-03 00:34:30.476 ParsingXMLTutorial[3308:c07]”
2013-04-03 00:34:30.477 ParsingXMLTutorial[3308:c07] 关注@jakemillermusic #jakemiller
2013-04-03 00:34:30.478 ParsingXMLTutorial [3308:c07] "
这是我用来获取 XML 格式的 URL:
这是解析代码:
-(id) loadXMLByURL:(NSString *)urlString
{
_tweets = [[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 foundCharacters:(NSString *)string
{
//string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; // space
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""]; // newline
string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
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:@"status"])
{
currentTweet = [Tweet alloc];
isStatus = YES;
}
if ([elementname isEqualToString:@"user"])
{
isStatus = NO;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:@"created_at"])
{
currentTweet.dateCreated = currentNodeContent;
}
if ([elementname isEqualToString:@"text"])
{
currentTweet.content = currentNodeContent;
}
}
if ([elementname isEqualToString:@"status"])
{
[self.tweets addObject:currentTweet];
currentTweet = nil;
currentNodeContent = nil;
}
}
@end