今天我们使用NSXMLParser这个类解析XML。
解析的类必须遵守NSXMLParserDelegate代理。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"111.xml" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
NSXMLParser *parse = [[NSXMLParser alloc] initWithData:data];
parse.delegate = self;
[parse parse];
}
设置parse.delegate = self;
- (void)parserDidStartDocument:(NSXMLParser *)parser{}
- (void)parserDidEndDocument:(NSXMLParser *)parser{}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
if([@"videos" isEqualToString:elementName]){
self.videos = [NSMutableArray array];
}else if([@"video" isEqualToString:elementName]){
// HMVideo *v1 = [HMVideo videmoWithDict:attributeDict];
// [self.videos addObject:v1];
NSLog(@"123");
UIButton *btn = [[UIButton alloc] init];
[self.videos addObject:btn];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{}
成员变量:@property(nonatomic,strong)NSMutableArray *videos;
touchBegin
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%ld",self.videos.count);
}
具体xml文件以及实际解析过程请看demo,
https://gitee.com/johnson__save_admin/xmldemo
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)