@interface ViewController ()<NSXMLParserDelegate>
@end
-(void)parserXml{
// 0.请求路径
NSURL *url = [NSURL URLWithString:@"xmlUrl"];
// 1.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 创建XML解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 设置代理
parser.delegate = self;
// 开始解析XML
[parser parse];
}];
}
pragma mark - <NSXMLParserDelegate>
* 解析到某个元素的结尾(比如解析</videos>)
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{}
* 解析到某个元素的开头(比如解析<videos>)
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"videos"]) return;
}
* 开始解析XML文档
-(void)parserDidStartDocument:(NSXMLParser *)parser{}
* 解析完毕
-(void)parserDidEndDocument:(NSXMLParser *)parser{}