博客> NSXNLParser 文件解析
NSXNLParser 文件解析
2018-10-18 12:14 评论:0 阅读:849 jijing2013
ios NSXMLParser SAX&DOM

1、NSXMLParser SAX 大文件 //1、打开文档

  • (void)parserDidStartDocument:(NSXMLParser *)parser //2、开始查找起始标签
  • (void)parser:(NSXMLParser )parser didStartElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName attributes:(NSDictionary *)attributeDict //3、获取内容
  • (void)parser:(NSXMLParser )parser foundCharacters:(NSString )string{ //防止多次执行获取内容的方法 [self.mutString appendString:string]; } //4、结束标签
  • (void)parser:(NSXMLParser )parser didEndElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName //5、查询文档结束
  • (void)parserDidEndDocument:(NSXMLParser *)parser{ //更新内容 [self.tableView reloadData]; }

2、NSXMLParser SAX 解析封装 block: 传回一个数组 typedef void(XMLTrainParser)(NSArray *trainArray);

//单例方法

  • (CYYTrainParser *)sharedTrainParser;

(void)getTrainListWithData:(NSData *)data parser:(XMLTrainParser)endParser; //单例的写法 //1、创建静态变量 //2、只alloc一次

  • (CYYTrainParser )sharedTrainParser{ static CYYTrainParser sharedIntance = nil; static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{ sharedIntance = [[self alloc] init]; }); return sharedIntance; } //调用方法

  • (void)getTrainListWithData:(NSData *)data parser:(XMLTrainParser)endParser{ _endParser = endParser;

//XML解析 NSXMLParser *parse = [[NSXMLParser alloc] initWithData:data];

parse.delegate = self;

[parse parse]; } viewController中调用 //调用block快 [[CYYTrainParser sharedTrainParser] getTrainListWithData:data parser:NSArray *trainArray { self.dataList = trainArray; //更新 [self.tableView reloadData]; }];

代理方法: 声明协议: @protocol trainParserDelegate

(void)trainParser:(CYYTrainParser )parser trainList:(NSArray )trainArray; @end

//delegate方法

  • (void)getTrainParserWithData:(NSData *)data parserDelegate:(id)delegate{ _delegate = delegate;

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; parser.delegate = self; [parser parse]; }

//将数据传回给视图控制器 if ([self.delegate respondsToSelector:@selector(trainParser:trainList:)]) { [self.delegate trainParser:self trainList:self.dataList]; } 3、XML解析 (DOM)适用于大文件 解析方式是从外到里一层一层解析 第三方文件GDataXMLNode //根据数据生成xml解析文档 GDataXMLDocument gdata = [[GDataXMLDocument alloc] initWithData:data error:nil]; //获取xml文档的根节点,遍历根节点下的所有子节点 for (GDataXMLElement element in gdata.rootElement.children) { // NSLog(@%@,element.children); //遍历子节点,获取孙节点 for (GDataXMLElement trainInfo in element.children) { NSLog(@%@,trainInfo.name); //创建model SXTTrainInfo train = [[SXTTrainInfo alloc] init]; //遍历孙节点下的子节点,并获取name值和stringValue for (GDataXMLElement note in trainInfo.children) { // NSLog(@%@,note.name); // NSLog(@%@,note.attributes); // //获取子节点的值 [train setValue:note.stringValue forKey:note.name]; } //// NSLog(@%@,trainInfo.attributes); //获取孙节点下的属性值 for (GDataXMLNode att in trainInfo.attributes) { [train setValue:att.stringValue forKey:att.name]; } [self.dataList addObject:train]; } }

NSLog(@"%@",self.dataList);

4、苹果自带的下拉刷新方法 //设置下拉刷新 UIRefreshControl *refresh = [[UIRefreshControl alloc] init];

NSDictionary dict = @{NSForegroundColorAttributeName:[UIColor redColor]}; NSAttributedString string = [[NSAttributedString alloc] initWithString:@"帮你署最新。。" attributes:dict]; refresh.attributedTitle = string;

[self.tableView addSubview:refresh];

收藏
0
sina weixin mail 回到顶部