说实话,神他么用过xml解析,只是调了几次这种接口,每次都要写,好麻烦,自己封装一下,以后会有用吧。
首先是基类
#import <Foundation/Foundation.h>
@interface XmlParsemanager : NSObject<NSXMLParserDelegate>
+(instancetype) shareInstance;
+(id) allocWithZone:(struct _NSZone *)zone;
@property (nonatomic,strong) NSMutableDictionary *resultDic;
@property (nonatomic,strong) NSString *currentElementRet;
@property (nonatomic,strong) NSString *currentElementVal;
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;
//
- (void)initset;
//请求数据的自定义
- (NSDictionary *)firstApplyWith:(NSDictionary *)parameters andUrl:(NSString *)url;
@end
#import "XmlParsemanager.h"
@implementation XmlParsemanager
static XmlParsemanager* _instance = nil;
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
[_instance initset];
}) ;
return _instance ;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
return [XmlParsemanager shareInstance] ;
}
- (void)initset{
self.manager = [AFHTTPRequestOperationManager manager];
AFXMLParserResponseSerializer * responseSer = [AFXMLParserResponseSerializer new];
self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
[self.manager setResponseSerializer:responseSer];
}
- (NSDictionary *)firstApplyWith:(NSDictionary *)parameters andUrl:(NSString *)url{
[self.manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject){
NSXMLParser * parser = (NSXMLParser *)responseObject;
parser.delegate = self;
[parser setShouldProcessNamespaces:YES];
//此法可行,但是处理麻烦
[parser parse];
if ([self.resultDic[@"Ret"] isEqualToString:@"0"] ) {
NSLog(@"登录成功!");
}else{
if ([self.resultDic[@"Ret"] isEqualToString:@"1"]) {
NSLog(@"登录失败!");
}else if ([self.resultDic[@"Ret"] isEqualToString:@"2"]){
NSLog(@"密码错误!");
}
}
NSLog(@"requestFlagshipStoreData xml:%@",parser);
} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"requestFlagshipStoreData Error:%@",error);
}];
return self.resultDic;
}
#pragma mark - NSXMLParserDelegate委托处理
//在文档开始的时候触发
-(void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"解析开始!");
}
//解析起始标记
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"解析到一个元素的开头---%@",elementName);
// 根据需求的标签获取相关的数据
if ([elementName isEqualToString:@"Ret"]) {
// 在这里需要赋值
[self.resultDic setValue:@"占位数据Ret" forKey:@"Ret"];
// 将当前的标签值传给声明的标签属性
self.currentElementRet = elementName;
}
if ([elementName isEqualToString:@"Val"]) {
[self.resultDic setValue:@"占位数据Val" forKey:@"Val"];
// 将当前的标签值传给声明的标签属性
self.currentElementVal = elementName;
}
}
//解析文本节点
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"值:%@",string);
// 从数组中取出相关的Student对象,每次取数组中最后的一个元素,保证值为最新的对象内容
if ([self.currentElementRet isEqualToString:@"Ret"]) {
// KVC
[self.resultDic setValue:string forKey:self.currentElementRet];
}
if ([self.currentElementVal isEqualToString:@"Val"]) {
// KVC
[self.resultDic setValue:string forKey:self.currentElementVal];
}
}
//解析结束标记
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"解析到一个元素的结尾---%@",elementName);
self.currentElementRet = nil;
self.currentElementVal = nil;
}
//文档结束时触发
-(void) parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"解析结束!");
NSLog(@"Ret = %@",self.resultDic[@"Ret"]);
}
@end