XML文件解析---SAX方法

前端之家收集整理的这篇文章主要介绍了XML文件解析---SAX方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>@H_502_4@ @H_502_4@ <books>@H_502_4@ @H_502_4@ <book name="book1" num="1" > 出版于1991年 </book>@H_502_4@ <book name="book2" num="2" > 出版于1992年 </book>@H_502_4@ <book name="book3" num="3" > 出版于1993年 </book>@H_502_4@ <book name="book4" num="4" > 出版于1994年 </book>@H_502_4@ @H_502_4@ </books>

@H_502_4@

解析后的结果:

@H_502_4@

@H_502_4@

@H_502_4@

封装model来接收解析后的XML文件数据

JRBook.h文件@H_502_4@

@H_502_4@ @H_502_4@ #import <Foundation/Foundation.h>@H_502_4@ @H_502_4@ @interface JRBook : NSObject@H_502_4@ @H_502_4@ //书籍名称@H_502_4@ @property (nonatomic,copy)NSString * name;@H_502_4@ @H_502_4@ @H_502_4@ //书籍数量@H_502_4@ @property (nonatomic,assign) NSInteger number;@H_502_4@ @H_502_4@ @H_502_4@ //出版年份@H_502_4@ @property (nonatomic,copy) NSString * punDate;@H_502_4@ @H_502_4@ @H_502_4@ + (JRBook *) getBookWithDic:(NSDictionary *) dic;@H_502_4@ @H_502_4@ @end@H_502_4@

@H_502_4@

@H_502_4@

JRBook.m文件@H_502_4@

@H_502_4@

#import "JRBook.h"@H_502_4@ @H_502_4@ @implementation JRBook@H_502_4@ @H_502_4@ @H_502_4@ + (JRBook *) getBookWithDic:(NSDictionary *) dic@H_502_4@ {@H_502_4@ JRBook * book=[[JRBook alloc]init];@H_502_4@ book.name=dic[@"name"];@H_502_4@ book.number=[dic[@"num"] integerValue];@H_502_4@ @H_502_4@ return book;@H_502_4@ }@H_502_4@ @H_502_4@ @end@H_502_4@

@H_502_4@

@H_502_4@

ViewController.m文件@H_502_4@ #import "ViewController.h" #import "JRBook.h" @interface ViewController () <NSXMLParserDelegate> { JRBook * tempBook; NSMutableString * tempPublic; } /** 存放model 的数组 */ @property (nonatomic,strong) NSMutableArray * dataArray; @end @implementation ViewController #pragma mark - 懒加载 - (NSMutableArray *) dataArray { if (_dataArray==nil) { _dataArray=[NSMutableArray array]; } return _dataArray; } #pragma mark - 程序入口 - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - 点击屏幕开始解析XML文件 - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //1>>> 初始化parse对象 NSString * path=[[NSBundle mainBundle]pathForResource:@"xml.xml" ofType:nil]; NSData * data=[NSData dataWithContentsOfFile:path]; NSXMLParser * parse=[[NSXMLParser alloc]initWithData:data]; //2>>> 设置代理,监控解析 parse.delegate=self; //3>>> 开始解析 [parse parse]; //4>>> for(JRBook * book in self.dataArray) { NSLog(@"书籍名称: %@-----书籍数量: %li----出版年份:%@",book.name,book.number,book.punDate); } } #pragma mark - 实现 NSXMLParserDelegate 的代理方法 - (void)parserDidStartDocument:(NSXMLParser *)parser { //NSLog(@"start"); } - (void)parserDidEndDocument:(NSXMLParser *)parser { //NSLog(@"end"); } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"book"]) { tempPublic=[NSMutableString string]; tempBook =[JRBook getBookWithDic:attributeDict]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"book"]) { tempBook.punDate=tempPublic; [self.dataArray addObject:tempBook]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { [tempPublic appendString:string]; } @end

猜你在找的XML相关文章