您好,我正在解析 RSS 和 Atom 提要,我的问题是如何检查 <description> 中的 <img> 和 <url> 标签?
必须有某种检查。谢谢。
这是我解析它们的方法:
- (NSArray *)parseFeed:(NSURL *)feedURL{
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:feedURL];
GDataXMLDocument *xmlParse = [[GDataXMLDocument alloc] initWithData:data error:&error];
GDataXMLElement *rootElement = xmlParse.rootElement;
NSArray *array = [[NSArray alloc] init];
if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
array = [self parseRSSFeed:rootElement];
return array;
} else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {
array = [self parseAtomFeed:rootElement];
return array;
} else {
NSLog(@"Unsupported root element: %@", rootElement.name);
return nil;
}
}
-(NSArray *)parseRSSFeed:(GDataXMLElement *) rootElement
{
NSMutableArray *entries = [[NSMutableArray alloc] init];
NSArray *channels = [rootElement elementsForName:@"channel"];
for (GDataXMLElement *channel in channels) {
NSArray *items = [channel elementsForName:@"item"];
for (GDataXMLElement *item in items) {
FeedItem *itemF = [[FeedItem alloc] init];
itemF.title = [item valueForChild:@"title"];
itemF.description = [item valueForChild:@"description"];
NSLog(@"IMAGE - %@", [item valueForChild:@"img"]);
itemF.dateString = [item valueForChild:@"pubDate"];
itemF.link = [NSURL URLWithString:[item valueForChild:@"link"]];
itemF.dateString = [item valueForChild:@"updated"];
itemF.author = [item valueForChild:@"author"];
[entries addObject:itemF];
NSLog(@"RSS - %@", itemF.title);
}
}
NSArray *RSSArray = [entries copy];
return RSSArray;
}
-(NSArray *)parseAtomFeed:(GDataXMLElement *) rootElement
{
NSMutableArray *entries = [[NSMutableArray alloc] init];
NSArray *entry = [rootElement elementsForName:@"entry"];
for (GDataXMLElement *entryElement in entry) {
// NSArray *items = [channel elementsForName:@"item"];
//for (GDataXMLElement *item in items) {
FeedItem *itemF = [[FeedItem alloc] init];
itemF.title = [entryElement valueForChild:@"title"];
itemF.description = [entryElement valueForChild:@"summary"];
NSArray *links = [entryElement elementsForName:@"link"];
for (GDataXMLElement *link in links) {
itemF.link = [NSURL URLWithString:[[link attributeForName:@"href"] stringValue]];
}
itemF.dateString = [entryElement valueForChild:@"updated"];
NSArray *authors = [entryElement elementsForName:@"author"];
for (GDataXMLElement *authorElement in authors) {
itemF.author = [authorElement valueForChild:@"name"];
}
[entries addObject:itemF];
NSLog(@"Atom - %@", itemF.title);
}
NSArray *atomArray = [entries copy];
return atomArray;
}
我正在使用 GDataXMLParser 和我自己的解析器类来解析它们。