XML解析出错处理

前端之家收集整理的这篇文章主要介绍了XML解析出错处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

正常解析完毕之后会直接走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

猜你在找的XML相关文章