Oc NSXMLParse(SAX)解析表格
2017-10-10 本文已影响0人
Wang99
手写xml文件
<?xml version="1.0" encoding="utf-8"?>
<root>
<hero>
<name>赏金猎人</name>
<like>枪林弹雨</like>
</hero>
<hero>
<name>提莫</name>
<like>老司机</like>
</hero>
<hero>
<name>流浪</name>
<like>禁锢</like>
</hero>
</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>
{
NSMutableArray *_arr;
//记录开始标签的名字
NSString *_newElementName;
//英雄的对象
Hero *myHero;
UITableView *_table;
}
@end
#define TEST_URL @"http://127.0.0.1/text.xml"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLRequest *reqeust = [[NSURLRequest alloc]initWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:reqeust returningResponse:nil error:nil];
//sax解析
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
//设置代理
parser.delegate = self;
//开始解析
[parser parse];
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
}
//开始解析自动回调
- (void)parserDidStartDocument:(NSXMLParser *)parser{
//初始化数组
_arr = [[NSMutableArray alloc]init];
}
//遇到开始标签自动回调
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{
//如果开始标签是hero 则初始化Hero对象
if ([elementName isEqualToString:@"hero"]) {
//初始化hero对象
myHero = [[Hero alloc]init];
//将myHero对象添加到数组中
[_arr addObject:myHero];
}
//记录开始标签的名字
_newElementName = elementName;
}
//遇到内容自动回调 在解析过程中遇到空格或者回车都会被当做内容调用该方法
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//如果开始标签是name则把获得到的内容赋值给hero的name
if ([_newElementName isEqualToString:@"name"]) {
myHero.name = string;
}else if([_newElementName isEqualToString:@"like"]){
myHero.like = string;
}
}
//遇到结束标签自动回调
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
_newElementName = nil;
}
//解析结束自动回调
- (void)parserDidEndDocument:(NSXMLParser *)parser{
//刷新表格
[_table reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arr.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];
}
cell.textLabel.text = [[_arr objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[_arr objectAtIndex:indexPath.row] like];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end