B1: Implement <NSXMLParserDelegate>
PHP Code:
@interface XeHoiViewController : UITableViewController<NSXMLParserDelegate>{
NSMutableArray *dsXeHoi;
BOOL got;
}
PHP Code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xehoi" ofType:@"xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
BOOL success = [xmlParser parse];
f(success)
NSLog(@"Thành công");
else
NSLog(@"Có lỗi");
B3: Tạo 3 function để bóc tách thẻ trong file XML
- Thẻ mở <ten>
PHP Code:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"ten"]){
got = YES;
}
}
PHP Code:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(got==YES){
NSLog(@"%@", string);
}
}
PHP Code:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"ten"]){
got = NO;
}
}
No comments:
Post a Comment