1

我是 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];
}

提前致谢。

4

1 回答 1

2

使用以下代码:

xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://hrms.atrity.info/api/people"]];

导致parser:parseErrorOccurred:被调用http://hrms.atrity.info/api/peopleURL。传递给该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];
于 2013-10-25T13:06:45.633 回答