大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家分享的是有关IOS如何解析XML文件 的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的彭水苗族土家族网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!这里使用NSXMLParser来解析,这个是apple自带的xml解析库
在Xcode中加入一个xml文件:
001 John Reminder Don't forget the meeting! 002 Jack cc how are you! 003 Tom bb I am fine!
先读出来
NSString* path = [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path options:(NSDataReadingMappedIfSafe) error:nil];
NSXMLParser *parser=[[NSXMLParser alloc] initWithData:tmpdata];
[parser setDelegate:self];
[parser parse];
[parser release];
[self parseShopListFromResponse:data];
然后进行解析:
#pragma mark NSXMLParser delegate methods - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict { self.currentTag = elementName; if ([elementName isEqualToString:@"root"]) { self.tmpList = [[NSMutableArray alloc] init]; }else if ([elementName isEqualToString:@"Shop"]) { self.tmpShop = [[ShopData alloc] init]; NSArray* array = [attributeDict allKeys]; NSString* key = [array lastObject]; NSString*s = [attributeDict objectForKey:key]; self.tmpShop.info = s; } } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if (self.currentString == nil) { self.currentString = [[NSMutableString alloc] initWithString:@""]; } if ([self.currentTag isEqualToString:@"name"] || [self.currentTag isEqualToString:@"id"] || [self.currentTag isEqualToString:@"url"] || [self.currentTag isEqualToString:@"info"]) { [self.currentString appendString:string]; } } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"Shop"]) { [self.tmpList addObject:self.tmpShop]; [self.tmpShop release]; } if ([elementName isEqualToString:@"name"]) { self.tmpShop.name = [self.currentString copy]; [self.currentString setString:@""]; }else if ([elementName isEqualToString:@"id"]) { self.tmpShop._id = [self.currentString copy]; [self.currentString setString:@""]; }else if ([elementName isEqualToString:@"url"]) { self.tmpShop.url = [self.currentString copy]; [self.currentString setString:@""]; }else if ([elementName isEqualToString:@"info"]) { // self.tmpShop.info = [self.currentString copy]; // [self.currentString setString:@""]; } } - (void)parserDidStartDocument:(NSXMLParser *)parser { NSLog(@"开始解析xml文件"); } - (void)parserDidEndDocument:(NSXMLParser *)parser { [self.tableView reloadData]; NSLog(@"解析xml文件完成"); }
Shopdata的结构是:
@property (nonatomic,retain) NSString *name; @property (nonatomic,retain) NSString *url; @property (nonatomic,retain) NSString *_id; @property (nonatomic,retain) NSString *info; @property (nonatomic, retain) UIImage *appIcon;
感谢各位的阅读!关于“IOS如何解析XML文件 ”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!