gpt4 book ai didi

iphone - 解析xml内容标签

转载 作者:行者123 更新时间:2023-11-29 04:48:21 24 4
gpt4 key购买 nike

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

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

代码如下:BOOL 已处理 = NO; 如果(当前文本){

    // 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; }
}

调用方法:

//自定义表格 View 单元格的外观。- (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;

}

最诚挚的问候

最佳答案

在解析每个元素时,由于换行/制表符/回车符,解析器将返回一个空白字符作为响应。如果您已经实现了解析器的回调方法(委托(delegate)),则检查空值,并且我不知道您在哪里实现了第一个片段(从某种意义上说,哪个解析器委托(delegate)方法有这个)。

关于iphone - 解析xml内容标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9276644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com