使用原生的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

猜你在找的XML相关文章