NSXMLParser est utilisé ici pour analyser. Il s'agit de la propre bibliothèque d'analyse XML d'Apple. Il existe un article de référence : http://www.php.cn/
Ajouter un fichier XML dans Xcode :
.<?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>
Lisez-le d'abord
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];
Puis analysez :
#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文件完成"); }
La structure de Shopdata est :
@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;
Ce qui précède est le contenu de l'analyse des fichiers XML par IOS. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois (www.php.cn) !