我是 ios 开发的新手,我需要将 xml 数据解析成一个数组,我尝试了一个教程,但我没有获取数据,当使用“ hrms.atrity.info/api/people”XML url 编辑时,解析器委托不工作而使用上面的链接执行。这是我的编码,它适用于“ image.apple.com/main/rss/hotnews/hotnews.rss ”。如何纠正这个探针,
- (void)viewDidLoad
{
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://hrms.atrity.info/api/people"];//NOt Working
NSURL *url = [NSURL
URLWithString:@"http://image.apple.com/main/rss/hotnews/hotnews.rss"];//This Works
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"
forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"FirstName"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:@"Users"])
{
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"Users"])
{
[item setObject:title forKey:@"FirstName"];
[item setObject:link forKey:@"LastName"];
[feeds addObject:[item copy]];
NSLog(@"%@",feeds);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([element isEqualToString:@"FirstName"])
{
[title appendString:string];
}
else if ([element isEqualToString:@"LastName"])
{
[link appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self.tableView reloadData];
}
提前致谢。