我正在尝试使用TBXMLParser解析此Feed:http://missing.amberalertnederland.nl/nl/index.rss。为此目的我使用此代码:
- (void)loadRecords:(NSString *)records {
NSString *someXML = @"http://missing.amberalertnederland.nl/nl/index.rss";
TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain];
records = [NSMutableArray array];
[records retain];
if (tbxml.rootXMLElement)
[self traverseElement:tbxml.rootXMLElement];
[tbxml release];
}
- (void) traverseElement:(TBXMLElement *)element {
do {
if (element->firstChild)
[self traverseElement:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:@"item"]) {
TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:element];
TBXMLElement *link = [TBXML childElementNamed:@"link" parentElement:element];
TBXMLElement *description = [TBXML childElementNamed:@"description" parentElement:element];
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:title],
[TBXML textForElement:link],
[TBXML textForElement:description],nil]];
}
} while ((element = element->nextSibling));
NSLog(@"%@", records);
}
但是,records数组只返回(null)值。
任何有关如何使用TBXML的指南(我认为我做得正确)也会受到欢迎。
答案 0 :(得分:1)
我从未使用过TBXML,我使用SMXMLDocument
以下是我用于解析RSS的方法:
- (NSArray *) arrayNewsItemsFromDocument:(SMXMLDocument *)document {
SMXMLElement *channel = [document.root childNamed:@"channel"];
NSArray *itemArray = [channel childrenNamed:@"item"];
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:[itemArray count]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Mon, 11 Jul 2011 00:00:00 +0200
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"EN"] autorelease]];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss ZZZZ"];
for (SMXMLElement *item in itemArray) {
News *news = [[News alloc] init];
news.title = [item childNamed:@"title"].value;
NSString *text = [item childNamed:@"encoded"].value;
news.text = [self stringFromHTMLString:text];
news.articleUrl = [item childNamed:@"link"].value;
NSString *dateString = [item childNamed:@"pubDate"].value;
NSDate *date = [dateFormatter dateFromString:dateString];
news.date = date;
[mutableArray addObject:news];
[news release], news = nil;
}
[dateFormatter release], dateFormatter = nil;
NSArray *newsArray = [[mutableArray copy] autorelease];
[mutableArray release], mutableArray = nil;
return newsArray;
}