你的位置:首页 > 操作系统

[操作系统]iOS IM开发准备工作(一)XML解析


  关于

  我们在哪些地方用现在json用的这么多,使用
  

  我写了一个工具类,先贴一下代码,下面会有一些说明

.h

 1 #import <Foundation/Foundation.h> 2  3 typedef NS_ENUM(NSInteger,// 这里我对我的 4    5    6 }; 7 @interface  8 // 这个是回调的Block 9 @property(nonatomic,copy)void (^returnParseArray)(NSArray * returnArray);10 @property(nonatomic,readonly)11 12 - (instancetype)initWithFilePath:(NSString *)path fileType:(NSString *)fileType modelName:(13 14 - (void)startWithFilePath:(NSString *)path fileType:(NSString *)fileType;15 @end

.m

 1 #import "" 2 #import "FaceModel.h" 3 #import "ToolClient.h" 4 #import "ServerModel.h" 5 @implementation  6   NSMutableArray * faceArray; 7   NSMutableArray * serverArray; 8 } 9 10 @synthesize currentModelName;11 - (instancetype)initWithFilePath:(NSString *)path fileType:(NSString *)fileType modelName:(12   self = [super init];13   if(self){14     currentModelName = modelName;15   }16   return self;17 }18 19 - (void)startWithFilePath:(NSString *)path fileType:(NSString *)fileType {20   [self parseWithPath:path type:fileType];21 }22 //23 - (void)parseWithPath:(NSString *)filePath type:(NSString *)fileType{24   if (currentModelName == 25     faceArray = [[NSMutableArray alloc]init];26   }else if(currentModelName == 27     serverArray = [[NSMutableArray alloc]init];28   }29   NSData * [[NSData alloc] initWithContentsOfFile:filePath];30   if(10){31     NS [[NS32     [parser setShouldProcessNamespaces:NO];33     [parser setShouldReportNamespacePrefixes:NO];34     [parser setShouldResolveExternalEntities:NO];35     [parser setDelegate:self];36     BOOL success = [parser parse];37     if(success) {38       [self parseSuccess];39     }else {40       [ToolClient activityShowMessage:@"" inView:[UIApplication sharedApplication].windows[0]];41     }42   }else {43     [ToolClient activityShowMessage:@"" inView:[UIApplication sharedApplication].windows[0]];44   }45 }46 // 成功后的回调47 - (void)parseSuccess {48   if(self.returnParseArray){49     if (currentModelName == 50       self.returnParseArray(faceArray);51     }else if(currentModelName == 52       self.returnParseArray(serverArray);53     }54   }55 }56 #pragma mark - NS57 58 - (void)parser:(NS)elementName59  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName60   attributes:(NSDictionary *)attributeDict {61   //  NSLog(@"Name:%@",elementName);62   63   if([elementName isEqualToString:@"face"] && currentModelName == 64     FaceModel * faceModel = [[FaceModel alloc]init];65     faceModel.kID = [attributeDict[@"id"]intValue];66     faceModel.kName = attributeDict[@"name"];67     faceModel.kImage = attributeDict[@"file"];68     [faceArray addObject:faceModel];69     faceModel = nil;70   }else if([elementName isEqualToString:@"Server"] && currentModelName == 71     ServerModel * sModel = [[ServerModel alloc]init];72     sModel.serverName = attributeDict[@"name"];73     sModel.serverIP = attributeDict[@"ChatServerIP"];74     sModel.chatPort = attributeDict[@"chatPort"];75     sModel.fileServerIP = attributeDict[@"fileServerIP"];76     sModel.filePort = attributeDict[@"filePort"];77     [serverArray addObject:sModel];78     sModel = nil;79   }80 }81 - (void)parser:(NSstring {82   //  NSLog(@"value:%@",string);83 }84 - (void)parserDidEndDocument:(NS)parser {85   //86   //  NSLog(@"%@",faceArray);87   88 }89 - (void)parser:(NS)qName{90   //  NSLog(@"elementName:%@",elementName);91   //  NSLog(@"qualifiedName:%@",qName);92   //93   //  NSLog(@"NS94   //  NSLog(@"%@",faceArray);95   //  NSLog(@"%i",(int)faceArray.count);96   97 }98 @end

现在我来说一下

 1 [parser setShouldProcessNamespaces:NO]; 2 [parser setShouldReportNamespacePrefixes:NO]; 3 [parser setShouldResolveExternalEntities:NO]; 

  第一个 setShouldProcessNamespaces 这个属性设置为YES的话,这两个方法会有值输出:parser:didStartElement:namespaceURI:qualifiedName:attributes: 和 parser:didEndElement:namespaceURI:qualifiedName: 这两个在解析过程中都是都是可以看到里面的节点或字段的名字的。我觉得调试的时候可以用一下。

  第二个 setShouldReportNamespacePrefixes 这个属性设置为YES的话,这两个方法会有值输出:parser:didStartMappingPrefix:toURI: 和 parser:didEndMappingPrefix: 这个我觉得完全没有必要用它

  第三个 setShouldResolveExternalEntities 这个属性设置为YES的话,这个方法会有值输出:parser:foundExternalEntityDeclarationWithName:publicID:systemID: 其中publicID 和systemID 都是

You may access this property once a parsing operation has begun or after an error occurs.

也就是当

  上面的代码是工具类,下面这段会告诉你这段代码怎么用:

 1 // 解析文件  2 - (void)parseFile:(NSString *)filepath{ 3   NSLog(@"filepath%@",filepath);// 文件的路径 4   if(data.length>10){// 简单的长度检测 5     __weak SelectServerViewController * ws = self;// 弱引用 6     // do parse 7     @"" modelName: 8     // 先设置回调 9     xp.returnParseArray = ^(NSArray * array){10       // 回调的结果 去给tableView 展示11       [ws gotDataArray:array];12     };13     // 再开始解析14     [xp startWithFilePath:filepath fileType:@""];15   }16  17 }18 19 - (void)gotDataArray:(NSArray *)array {20   if(array){21 //    NSLog(@"array:%@",array);22     dataArray = [array mutableCopy];23     [myTableView reloadData];24   }25 }