我是ios开发的新手,我需要将xml数据解析成一个数组,我尝试了一个教程,并且在使用“ hrms.atrity.info/api/people ” XML url,Parser代表在使用上述链接执行时不工作。这是我的编码,它适用于“ 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];
}
先谢谢。
答案 0 :(得分:2)
使用以下代码:
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://hrms.atrity.info/api/people"]];
导致为parser:parseErrorOccurred:
网址调用http://hrms.atrity.info/api/people
。传递给该代理人的NSError
会显示错误代码4
,该代码会转换为NSXMLParserEmptyDocumentError
。这是因为URL返回的是JSON而不是XML。您可以使用以下代码进行检查:
NSError *error;
NSString *data = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://hrms.atrity.info/api/people"] encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", data);
您可以在Xcode的控制台中清楚地看到数据是JSON。要以XML格式检索数据,您需要指定Accept
标头,其值为application/xml
。您不能仅使用NSURL
执行此操作,并且需要使用NSMutableURLRequest
以便您可以设置自定义标头值,如下所示:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://hrms.atrity.info/api/people"]];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
xmlParser = [[NSXMLParser alloc] initWithData:data];