1. 程式人生 > >使用原生的NSXmlParser解析xml資料

使用原生的NSXmlParser解析xml資料

xml檔案地址: http://d1xzuxjlafny7l.cloudfront.net/downloads/Party.xml

下面是本人使用原聲NSXmlParser對xml資料進行的解析。

這裡有篇有一篇很不錯的教程,使用GDataXml庫對xml資料進行的解析,而且還有對xml資料格式的組建,講解的很是詳細。


#import "ViewController.h"

#pragma mark ---- xml解析器

/**
 *  XML資料解析器
 */
@interface XmlParser : NSObject<NSXMLParserDelegate>
@end

@interface XmlParser ()

//解析完成後呼叫的block
@property (nonatomic,copy) void(^completionBlock)(NSArray *xmlArray);
//儲存完整地資料結構
@property (nonatomic,strong) NSMutableArray *rootArray;

//臨時變數
@property (nonatomic,strong) NSMutableDictionary *tmpDictionary;
@property (nonatomic,copy) NSString *tmpElementName;
@end

static NSString *xmlUrlPath = @"http://d1xzuxjlafny7l.cloudfront.net/downloads/Party.xml";

@implementation XmlParser

-(void)startWithCompletion:(void (^)(NSArray *))completion{
    _completionBlock = completion;
    
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:xmlUrlPath]];
    parser.delegate = self;
    if( ![parser parse] ){
        NSLog(@"獲取指定路徑的xml檔案失敗!");
    }
}

//懶載入
-(NSMutableArray *)rootArray{
    if( !_rootArray ){
        _rootArray = [NSMutableArray array];
    }
    return _rootArray;
}
-(NSMutableDictionary *)tmpDictionary{
    if( !_tmpDictionary ){
        _tmpDictionary = [NSMutableDictionary dictionary];
    }
    return _tmpDictionary;
}

#pragma mark --- NSXmlParser delegate  由名知其意
- (void)parserDidStartDocument:(NSXMLParser *)parser{
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    //釋放臨時變數
    _tmpDictionary  = nil;
    
    //回撥block
    _completionBlock(_rootArray);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    _tmpElementName = elementName;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if( [elementName isEqualToString:@"Player"] ){
        [self.rootArray addObject:self.tmpDictionary];
    }
    //清空儲存的資料
    _tmpElementName = nil;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if( [_tmpElementName isEqualToString:@"Name"] || [_tmpElementName isEqualToString:@"Level"] || [_tmpElementName isEqualToString:@"Class"] ){
        [self.tmpDictionary setObject:string forKey:_tmpElementName];
    }
}


@end


#pragma mark --- Controller
//對XmlParser解析器的使用
@interface ViewController ()

@property (nonatomic,strong) XmlParser *xmlParser;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.xmlParser startWithCompletion:^(NSArray *xmlArray) {
        NSLog(@"xmlArray = %@",xmlArray);
    }];
    
}

-(XmlParser *)xmlParser{
    if( !_xmlParser ){
        _xmlParser = [[XmlParser alloc] init];
    }
    return _xmlParser;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end