您好我正在解析RSS和Atom提要,我的问题是如何检查< 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和我自己的解析器类解析它们。
答案 0 :(得分:1)
这样做:
NSString *Str=[item valueForChild:@"description"];
NSArray *tmp=[Str componentsSeparatedByString:@"<Img>"];
if([tmp count]>1){
NSString *urlstr=[[tmp objectatindex:1] stringByReplacingOccurrencesOfString:@" </Img>" withString:@""];
}
现在urlstr
包含您的图片网址。
享受
答案 1 :(得分:0)
我以为
itemF.description = [item valueForChild:@"description"];
是NSString
。因此,您可以将componentsSeparatedByString
与“您的代码”结合使用。
它将返回数组。在位置1的数组中,您将获得您的img url。
此网址具有结束括号“您的结束标记”。
用空格替换“你的结束标签”。你会得到图片网址。
希望,这会对你有帮助。