Home  >  Article  >  Backend Development  >  iOS parsing XML files

iOS parsing XML files

黄舟
黄舟Original
2017-02-20 14:47:291373browse

NSXMLParser is used here to parse. This is Apple’s own xml parsing library. There is a reference article: http://www.php.cn/

Add an xml file to Xcode:





    001
    John
    Reminder
    Don't forget the meeting!


    002
    Jack
    cc
    how are you!


    003
    Tom
    bb
    I am fine!




##Read it out first



   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];



Then parse:


#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文件完成");
}





#The structure of Shopdata is:

@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;

The above is the content of IOS parsing XML files. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn