正常解析完毕之后会直接走parserDidEndDocument这个方法
解析出错之后会执行一个相应的方法,- (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError,执行完毕此方法后.会根据解析出错的地方去执行方法parserDidEndDocument(有时候会执行,有时候不会执行)
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = [self loadTextXml];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
}
- (NSString *)loadTextXml
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"CdnSwitchDomain.xml"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath])
{
return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}
return nil;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"info"])
{
NSLog(@"parse info");
}
if([elementName isEqualToString:@"newSplashURL"])
{
NSLog(@"parse newSplashURL");
}
if([elementName isEqualToString:@"androidSplash"])
{
NSLog(@"parse androidSplash");
}
if([elementName isEqualToString:@"weatherDomains"])
{
NSLog(@"parse weatherDomains");
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"end");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"AN error occuse");
}
原文链接:https://www.f2er.com/xml/300184.html