解析xml内容标记

时间:2012-02-14 12:11:49

标签: iphone objective-c ios xml

我的问题是我无法解析xml文件中的标记。 它返回一个空值,我通过使用带有%@的NSLog来测试它。

如果有人能给我一个解决方案,我会非常感激。

这是代码: BOOL处理=否;     if(currentText){

    // Remove newlines and whitespace from currentText
    NSString *processedText = [currentText stringByRemovingNewLinesAndWhitespace];

    // Process
    switch (feedType) {
        case FeedTypeRSS: {

            // Item
            if (!processed) {
                if ([currentPath isEqualToString:@"/rss/channel/item/title"]) { if (processedText.length > 0) item.title = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/link"]) { if (processedText.length > 0) item.link = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/guid"]) { if (processedText.length > 0) item.identifier = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/description"]) { if (processedText.length > 0) item.summary = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/content:encoded"]) { if (processedText.length > 0) item.content = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/pubDate"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822]; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
            }

            // Info
            if (!processed && feedParseType != ParseTypeItemsOnly) {
                if ([currentPath isEqualToString:@"/rss/channel/title"]) { if (processedText.length > 0) info.title = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/description"]) { if (processedText.length > 0) info.summary = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rss/channel/link"]) { if (processedText.length > 0) info.link = processedText; processed = YES; }
            }

            break;
        }
        case FeedTypeRSS1: {

            // Item
            if (!processed) {
                if ([currentPath isEqualToString:@"/rdf:RDF/item/title"]) { if (processedText.length > 0) item.title = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/url"]) { if (processedText.length > 0) item.image = processedText; processed = YES; }
                else if([currentPath isEqualToString:@"/rss/channel/item/category"]) { if (processedText.length > 0) item.category = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/link"]) { if (processedText.length > 0) item.link = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/dc:identifier"]) { if (processedText.length > 0) item.identifier = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/description"]) { if (processedText.length > 0) item.summary = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/content:encoded"]) { if (processedText.length > 0) item.content = processedText; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
                else if ([currentPath isEqualToString:@"/rdf:RDF/item/enc:enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
            }

通话方法:

//自定义表格视图单元格的外观。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

// Configure the cell.
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
    NSLog(@"content = %@",item.content);//<-------------------------------------------Returns a NULL
    // Process
    NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";
    NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]";

    // Set
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.textLabel.text = itemTitle;
    NSMutableString *subtitle = [NSMutableString string];
    if (item.date) [subtitle appendFormat:@"%@: ", [formatter stringFromDate:item.date]];
    [subtitle appendString:itemSummary];
    cell.detailTextLabel.text = subtitle;

}
return cell;

}

最好的问候

1 个答案:

答案 0 :(得分:1)

在解析每个元素时,解析器将返回一个空格字符作为响应,因为新的行/制表符/回车符。如果你已经实现了解析器的回调方法(委托),那么检查空值,并且我不知道你实现的位置(在某种意义上,解析器委托方法有这个)的第一个片段。