0

所以我正在制作一个应用程序,其中一部分在表格视图中显示用户的推文。但是,某些推文会损坏某些内容,例如它仅显示单个字符,例如(“)或表情符号字符。例如,如果推文说:

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:

http://api.twitter.com/1/statuses/user_timeline/LexxiSaal.xml?include_entities=true&include_rts=true&screen_name=twitterapi&trim_user=false&contributor_details=true&count=50


这是解析代码:

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

1 回答 1

0

我会建议一些改变如下

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
     NSString *value=[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];

     if(currentNodeContent == nil){
         currentNodeContent = [[NSMutableString alloc] initWithString:value];
     }else
         [currentNodeContent appendString:value];
}

didEnd

- (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; REMOVED
     }

    currentNodeContent = nil; // PUT OUTSIDE
}

问题是您必须在方法 foundCharacters 中附加字符串,因为解析器不会一次返回标签之间的所有字符串..

最后,您应该在全局的 didEnd 方法中将currentNodeContent设为 nil,因为您也将它用于其他文本。

希望以上有帮助..

于 2013-04-04T04:34:10.190 回答