您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關IOS如何解析XML文件 的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
這里使用NSXMLParser來解析,這個是apple自帶的xml解析庫
在Xcode中加入一個xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <root> <Shop shoplocation="beijing"> <id>001</id> <name>John</name> <url>Reminder</url> <info>Don't forget the meeting!</info> </Shop> <Shop shoplocation="dongcheng"> <id>002</id> <name>Jack</name> <url>cc</url> <info>how are you!</info> </Shop> <Shop shoplocation="haidian"> <id>003</id> <name>Tom</name> <url>bb</url> <info>I am fine!</info> </Shop> </root>
先讀出來
NSString* path = [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"xml"]; NSData *data = [[NSData alloc] initWithContentsOfFile:path options:(NSDataReadingMappedIfSafe) error:nil]; NSXMLParser *parser=[[NSXMLParser alloc] initWithData:tmpdata]; <span style="white-space:pre"> </span>[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文件 ”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。