首先创建创建XML文档放进资源库替身里
报错解决方法
接下来就可以上代码了手下创建model类继承NSObject, .h创建两个属性,但是属性名要和参考XML命名
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *like;
接下来就可以在ViewController.h 写东西了,导入第三方及model类头文件!
#import "Student.h"
#import "GDataXML/GDataXMLNode.h"
解析地址
#define URL @"http://127.0.0.1/1505CSort.xml"
//创建表格
<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableDictionary *dicm;
//表格和解析都写在方法里 在viewDidLoad里面self调用
{
[self tableView];
[self parseringData];
}
#pragma mark 表格 - UITableViewDataSource
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
_tableView.backgroundColor = [UIColor orangeColor];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_tableView];
}
return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dicm.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *as = [_dicm objectForKey:[_dicm.allKeys objectAtIndex:section]];
return as.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
NSArray *sorearr = [_dicm objectForKey:[_dicm.allKeys objectAtIndex:indexPath.section]];
cell.textLabel.text = [sorearr[indexPath.row]name];
cell.detailTextLabel.text = [sorearr[indexPath.row]like];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_dicm.allKeys objectAtIndex:section];
}
//解析
-(void)parseringData
{
NSURL *url = [NSURL URLWithString:URL];//封装url对象
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];//封装请求对象
//发起异步网络连接
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *respone,NSData *data,NSError *connectionError){
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
GDataXMLElement *rooeElement = [doc rootElement];
NSArray *sorElement = [rooeElement elementsForName:@"sort"];
_dicm = [[NSMutableDictionary alloc]init];
for (GDataXMLElement *stuElment in sorElement)
{
//获取
NSString *key = [[stuElment attributeForName:@"kind"]stringValue];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (GDataXMLElement *ele in [stuElment elementsForName:@"hero"])
{
NSArray *temparr = [ele elementsForName:@"name"];
GDataXMLElement *Gdata = [temparr firstObject];
Student*stu = [[Student alloc] init];
stu.name = [Gdata stringValue];
stu.like = [[[ele elementsForName:@"like"]firstObject]stringValue];
[arr addObject:stu];
}
//
[_dicm setObject:arr forKey:key];
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"dic == %@",_dicm);
[_tableView reloadData];
});
}];
}
谢谢
网友评论