数据解析
关于iOS开发的中数据解析的方法有两种JSON和XML,这里只做简单的介绍,会使用就可以了。
JSON——
关于JSON的解析经过很多爱好者的分析使用相同自带的还是最好的,不管是从使用的容易度还是性能方面
NSJSONSerialization
1 -(void)start 2 { 3 4 NSString* path = [[NSBundle mainBundle] pathForResource:@"Notes" ofType:@"json"]; 5 6 NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path]; 7 8 NXJsonParser* jsonParser = [[NXJsonParser alloc] initWithData:jsonData]; 9 id jsonObj = [jsonParser parse:nil ignoreNulls:NO]; 10 11 if (!jsonObj) { 12 NSLog(@"JSON数据解析失败。"); 13 return; 14 } 15 16 _notes = [jsonObj objectForKey:@"Record"]; 17 18 [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil]; 19 self.notes = nil; 20 21 }
XML——
SAX:小数据的XML数据推荐使用NSXMLPaser
DOM(第三方库):大数据的XML数据使用最多的是TBXML和GDataXML,这两种方式使用思路差不多,TBXML更快但是具体使用哪一种看个人爱好和公司的需求
SAX——NSXMLParser
上图是XML的部分数据,就是我需要解析的XML文件对应的数据
NSXMLParser解析源码
1 -(void)start 2 { 3 NSString* path = [[NSBundle mainBundle] pathForResource:@"Notes" ofType:@"xml"]; 4 5 NSURL *url = [NSURL fileURLWithPath:path]; 6 //开始解析XML 7 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 8 parser.delegate = self; 9 [parser parse]; 10 NSLog(@"解析完成..."); 11 } 12 13 //文档开始的时候触发 14 - (void)parserDidStartDocument:(NSXMLParser *)parser 15 { 16 _notes = [NSMutableArray new]; 17 } 18 19 //文档出错的时候触发 20 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 21 { 22 NSLog(@"%@",parseError); 23 } 24 25 //遇到一个开始标签时候触发 26 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 27 namespaceURI:(NSString *)namespaceURI 28 qualifiedName:(NSString *)qualifiedName 29 attributes:(NSDictionary *)attributeDict 30 { 31 _currentTagName = elementName; 32 if ([_currentTagName isEqualToString:@"Note"]) { 33 NSString *_id = [attributeDict objectForKey:@"id"]; 34 NSMutableDictionary *dict = [NSMutableDictionary new]; 35 [dict setObject:_id forKey:@"id"]; 36 [_notes addObject:dict]; 37 } 38 39 } 40 41 //遇到字符串时候触发 42 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 43 { 44 //替换回车符和空格 45 string =[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 46 if ([string isEqualToString:@""]) { 47 return; 48 } 49 NSMutableDictionary *dict = [_notes lastObject]; 50 51 if ([_currentTagName isEqualToString:@"CDate"] && dict) { 52 [dict setObject:string forKey:@"CDate"]; 53 } 54 55 if ([_currentTagName isEqualToString:@"Content"] && dict) { 56 [dict setObject:string forKey:@"Content"]; 57 } 58 59 if ([_currentTagName isEqualToString:@"UserID"] && dict) { 60 [dict setObject:string forKey:@"UserID"]; 61 } 62 } 63 64 //遇到结束标签时候出发 65 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 66 namespaceURI:(NSString *)namespaceURI 67 qualifiedName:(NSString *)qName; 68 { 69 self.currentTagName = nil; 70 } 71 72 73 //遇到文档结束时候触发 74 - (void)parserDidEndDocument:(NSXMLParser *)parser 75 { 76 [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil]; 77 self.notes = nil; 78 }
读取数据的方式有:
initWithContentsOfUR:使用url获取文件数据(解析对象)
initWithData:使用NSData创建解析对象
initWithStream:使用IO流创建解析对象
DOM——TBXML
1 //开始解析 2 -(void)start 3 { 4 _notes = [NSMutableArray new]; 5 6 TBXML* tbxml = [[TBXML alloc] initWithXMLFile:@"Notes.xml" error:nil]; 7 8 TBXMLElement * root = tbxml.rootXMLElement; 9 10 // if root element is valid 11 if (root) { 12 13 TBXMLElement * noteElement = [TBXML childElementNamed:@"Note" parentElement:root]; 14 15 while ( noteElement != nil) { 16 17 NSMutableDictionary *dict = [NSMutableDictionary new]; 18 19 TBXMLElement *CDateElement = [TBXML childElementNamed:@"CDate" parentElement:noteElement]; 20 if ( CDateElement != nil) { 21 NSString *CDate = [TBXML textForElement:CDateElement]; 22 [dict setValue:CDate forKey:@"CDate"]; 23 } 24 25 TBXMLElement *ContentElement = [TBXML childElementNamed:@"Content" parentElement:noteElement]; 26 if ( ContentElement != nil) { 27 NSString *Content = [TBXML textForElement:ContentElement]; 28 [dict setValue:Content forKey:@"Content"]; 29 } 30 31 TBXMLElement *UserIDElement = [TBXML childElementNamed:@"UserID" parentElement:noteElement]; 32 if ( UserIDElement != nil) { 33 NSString *UserID = [TBXML textForElement:UserIDElement]; 34 [dict setValue:UserID forKey:@"UserID"]; 35 } 36 37 //获得ID属性 38 NSString *_id = [TBXML valueOfAttributeNamed:@"id" forElement:noteElement error:nil]; 39 [dict setValue:_id forKey:@"id"]; 40 41 [_notes addObject:dict]; 42 43 44 noteElement = [TBXML nextSiblingNamed:@"Note" searchFromElement:noteElement]; 45 46 } 47 } 48 49 NSLog(@"解析完成..."); 50 51 [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil]; 52 self.notes = nil; 53 54 }
initWithXMLFile:error:文件中
initWithXMLString:error:返回的字符串中
initWithData:error:NSData中
DOM——GDataXML
1 /** 2 * GDataXML解析:DOM----一口气加载所有,适合xml小文件 3 */ 4 -(void)gDataXML:(NSData *)data 5 { 6 // 加载整个XML数据 7 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; 8 9 // 获得文档的根元素 -- videos元素 10 GDataXMLElement *root = doc.rootElement; 11 12 // 获得根元素里面的所有video元素 13 NSArray *elements = [root elementsForName:@"video"]; 14 15 // 遍历所有的video元素 16 for (GDataXMLElement *videoElement in elements) { 17 iCocosVideo *video = [[iCocosVideo alloc] init]; 18 19 // 取出元素的属性 20 video.id = [videoElement attributeForName:@"id"].stringValue.intValue; 21 video.length = [videoElement attributeForName:@"length"].stringValue.intValue; 22 video.name = [videoElement attributeForName:@"name"].stringValue; 23 video.image = [videoElement attributeForName:@"image"].stringValue; 24 video.url = [videoElement attributeForName:@"url"].stringValue; 25 26 // 添加到数组中 27 [self.videos addObject:video]; 28 } 29 30 31 }
时间: 2024-10-10 20:38:00