手写xml文件
<?xml version="1.0" encoding="utf-8"?>
<root>
<sort kind = "ADC">
<hero>
<name>赏金猎人</name>
<like>枪林弹雨</like>
</hero>
<hero>
<name>寒冰射手</name>
<like>万箭齐发</like>
</hero>
<hero>
<name>皮城女警</name>
<like>让子弹飞</like>
</hero>
</sort>
<sort kind = "AP">
<hero>
<name>流浪法师</name>
<like>禁锢</like>
</hero>
<hero>
<name>提莫</name>
<like>种蘑菇</like>
</hero>
</sort>
<sort kind = "T">
<hero>
<name>德玛西亚</name>
<like>三爪</like>
</hero>
<hero>
<name>雷霆咆哮</name>
<like>枪林弹雨</like>
</hero>
<hero>
<name>皮夹龙骨</name>
<like>无线</like>
</hero>
</sort>
</root>
Model类
Hero.h
#import <Foundation/Foundation.h>
@interface Hero : NSObject
@property (nonatomic,strong)NSString *name,*like;
@end
控制器类
ViewController.m
#import "ViewController.h"
#import "Hero.h"
@interface ViewController ()<NSXMLParserDelegate,UITableViewDelegate,UITableViewDataSource>
{
NSMutableDictionary *_dic;
NSMutableArray *_arr;
NSString *_newElementName;
Hero *_hero;
UITableView *_table;
}
@end
#define TEST_URL @"http://127.0.0.1/textSort.xml"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
#if 0
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLRequest *requset = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:requset returningResponse:nil error:nil];
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
parser.delegate = self;
[parser parse];
#elif 1
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
parser.delegate = self;
[parser parse];
}];
//开始请求
[task resume];
[_table reloadData];
#endif
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
_table.delegate =self;
_table .dataSource = self;
[self.view addSubview:_table];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
_dic = [[NSMutableDictionary alloc]init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
if ([elementName isEqualToString:@"sort"])
{
//给字典设置key和值 以英雄类型为key,
NSString *key = [attributeDict objectForKey:@"kind"];
_arr = [[NSMutableArray alloc]init];
//给字典设置数值
[_dic setObject:_arr forKey:key];
}
else if ([elementName isEqualToString:@"hero"])
{
_hero = [[Hero alloc]init];
[_arr addObject:_hero];
}
_newElementName = elementName;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([_newElementName isEqualToString:@"name"])
{
_hero.name = string;
}
else if ([_newElementName isEqualToString:@"lile"])
{
_hero.like = string;
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
_newElementName = nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
}
//分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dic.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [_dic.allKeys objectAtIndex:section];
return [[_dic objectForKey:key]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
}
NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];
cell.textLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row ] name];
cell.detailTextLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] like];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_dic.allKeys objectAtIndex:section];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论