XML解析NSXMLParser

NSXMLParser

// 传入XML数据,创建解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 设置代理,监听解析过程
parser.delegate = self;
// 开始解析
[parser parse];

NSXMLParser采取的是SAX方式解析,特点是事件驱动,下面情况都会通知代理
当扫描到文档(Document)的开始与结束
当扫描到元素(Element)的开始与结束
NSXMLParserDelegate

当扫描到文档的开始时调用(开始解析)
- (void)parserDidStartDocument:(NSXMLParser *)parser

当扫描到元素的开始时调用(attributeDict存放着元素的属性)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

当扫描到元素的结束时调用
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

当扫描到文档的结束时调用(解析完毕)
- (void)parserDidEndDocument:(NSXMLParser *)parser

请求接口:

http://www.example.com:8080/videos?type=XML

接口返回的XML数据:



数据格式化:



    

定义Video模型:
Video.h

@interface Video : NSObject
/** ID */
@property (nonatomic, assign) NSInteger ID;
/** 视频名字 */
@property (nonatomic, copy) NSString *name;
/** 视频时长 */
@property (nonatomic, assign) NSInteger length;
/** 视频图片 */
@property (nonatomic, copy) NSString *image;
/** 视频文件路径 */
@property (nonatomic, copy) NSString *url;
@end

video.m
没有内容

NSXMLParser 解析XML文档:

/** 视频数据 */
@property (nonatomic, strong) NSMutableArray *videos;

- (NSMutableArray *)videos{
    if (!_videos) {
        _videos = [NSMutableArray array];
    }
    return _videos;
}

- (void)loadVideos{
    NSString *urlString = @"http://www.example.com:8080/videos?type=XML";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[[NSOperationQueue alloc] init]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (connectionError == nil) {
                                   // 创建XML解析器
                                   NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
                                   // 设置代理
                                   parser.delegate = self;
                                   // 开始解析XML
                                   [parser parse];
                               }
                           }];
}

#pragma mark - 
/**
 * 开始解析XML文档
 */
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidStartDocument");
}

/**
 * 解析到某个元素的开头(比如解析)
 */
- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict{
    
    NSLog(@"didStartElement - %@",elementName);
    if ([elementName isEqualToString:@"videos"]) return;
    Video *video = [[XMGVideo alloc] init];
    video.ID = (NSUInteger)[attributeDict valueForKey:@"id"];
    video.image = [attributeDict valueForKey:@"image"];
    video.length = (NSUInteger)[attributeDict valueForKey:@"length"];
    video.name = [attributeDict valueForKey:@"name"];
    video.url = [attributeDict valueForKey:@"url"];
    [self.videos addObject:video];
    [self.videos addObject:video];
}

/**
 * 解析到某个元素的结尾(比如解析)
 */
- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName{
    NSLog(@"didEndElement - %@", elementName);
}

/**
 * 解析完毕
 */
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"parserDidEndDocument");
    
    __weak typeof(self) weakSelf = self;
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [weakSelf.tableView reloadData];
    }];
}

你可能感兴趣的:(XML解析NSXMLParser)