使用单引号的NSXMLParser问题

时间:2014-06-10 11:24:36

标签: ios objective-c xcode nsxmlparser

它应该存储这个字符串: Jim的随机主题。但它停在单引号上。所以它存储了 Jim

<标题> Jim' s随机主题' s关于愚蠢的分号' s< /标题>

XMLParser.m (简体)

typedef enum MESSAGE_TYPE {
    TITLE = 1,
    NOTFIRSTTIME = 2,
    SEMICOLON = 3
} Message_Type;

NSString * title;
NSXMLParser *rssParser;
Message_Type messageType;

+ (void)initialize {
     messageType = UNDEFINED;
}

- (void) parseXML:(NSData *) rawxml{

    NSString* myString;
    NSData *data;

    myString = [[NSString alloc] initWithData:rawxml encoding:NSASCIIStringEncoding];
    myString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    data = [myString dataUsingEncoding:NSUTF8StringEncoding];

    rssParser = [[NSXMLParser alloc] initWithData:data];
    [rssParser setDelegate:self];
    [rssParser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"title"]) {
        messageType = TITLE;
    }
}

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

if(messageType == SEMICOLON) {
    title = [title stringByAppendingString:string];
    messageType = NOTFIRSTTIME;
}

if(messageType == NOTFIRSTTIME) {
    if([string isEqualToString:@"'"]) {
        title = [title stringByAppendingString:string];
        messageType = SEMICOLON;
    }
}

    if(messageType == TITLE) {
        title = string;
        NSLog(@"title = %@",title);
        messageType = NOTFIRSTTIME;
    }
}

更新:我已经用一个使用Enum的循环解决了我自己的问题,并在找到分号时附加了字符串。无论如何,谢谢你们。

1 个答案:

答案 0 :(得分:0)

是的,现在正在运作。我已经更新了上面的答案。