我想根据xml格式的标题和链接从网站获取更新。
我已尝试使用下面的代码,但它无效。在控制台中,它显示消息:
2011-11-03 14:45:05.987 tabbar [673:e903] * 终止应用程序 未捕获的异常'NSInvalidArgumentException',原因:' - [NSCFString isEqualtostring:]:无法识别的选择器发送到实例0x5746830'
如果我再次运行,表视图正在加载,但表格单元格中没有数据。
它在if ([elementName isEqualtostring:@"item"])
行显示此消息:
程序接收信号SIGABRT
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
classelement = elementName;
if ([elementName isEqualtostring:@"item"])
{
itemselected = YES;
multitle = [[NSMutableString alloc]init];
mullink = [[NSMutableString alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedname:(NSString *)qName
{
if ([elementName isEqualToString:@"item"])
{
itemselected = NO;
[titlearray addObject:multitle];
[linkarray addObject:mullink];
[multitle release];
[mullink release];
[self.tbl reloadData];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (itemselected)
{
if ([classelement isEqualToString:@"title"])
{
NSLog(@"%@",string);
[multitle appendString:multitle];
}
else if([classelement isEqualToString:@"link"])
{
[multitle appendString:string];
}
}
}
答案 0 :(得分:0)
如果您直接从网站上阅读,很可能会收到“无效”字符。一旦解析器看到这些无效字符,它就会崩溃。我建议使用“HPPLE Parser”。如果出现无效字符,它的效果会更好,不会崩溃。
这里的Hpple信息:http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/
答案 1 :(得分:0)
最后我明白了。我所做的是我已经使用了两个NSMutableArray
和两个NSMutableString
,我将这些项添加到字符串中,然后将这些字符串附加到两个数组中。
(void)viewDidLoad
{
titlearray = [[NSMutableArray alloc]init];
linkarray = [[NSMutableArray alloc]init];
NSString *rssaddress =@"http://www.greenday.com/rss";
NSURL *url = [NSURL URLWithString:rssaddress];
xmlparser = [[NSXMLParser alloc]initWithContentsOfURL:url];
[xmlparser setDelegate:self];
[xmlparser parse];
[super viewDidLoad];
}
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
classelement = elementName;
if([elementName isEqualToString:@"item" ])
{
itemselected = YES;
titlestrng = [[NSMutableString alloc]init];
linkstrng = [[NSMutableString alloc]init];
}
}
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"item" ])
{
itemselected = NO;
[titlearray addObject:titlestrng];
[linkarray addObject:linkstrng];
[titlestrng release];
[linkstrng release];
[self.tb reloadData];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(itemselected)
{
if([classelement isEqualToString:@"title"])
{
[titlestrng appendString: string];
NSLog(@"%@",titlestrng);
}
else if ([classelement isEqualToString:@"link"])
{
[linkstrng appendString:string];
NSLog(@"%@",linkstrng);
}
}
}