XML 解析 - NSXMLParserErrorDomain 错误 5 [英] XML Parsing - NSXMLParserErrorDomain error 5
问题描述
我正在尝试解析 XML 文件.它运作良好 - 直到今天......
I'm trying to parse a XML File. It worked very well - until today...
这是我开始解析 XML 的方式:
Here's how I start to parse the XML:
NSString *link = [[NSString alloc] init];
link = @"link_to_xml_file";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
这是我如何使用接收到的数据:
And here's how I'm using the received data:
- (void)connection:(NSURLConnection *)theConnection
didReceiveData:(NSData *)incrementalData
{
if (data == nil)
data = [[NSMutableData alloc] init];
[data appendData:incrementalData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",actual]];
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] error:&parseError];
if (parseError != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[parseError localizedDescription] delegate:nil cancelButtonTitle:@"Zurück" otherButtonTitles:nil];
[alert show];
[alert release];
} //shows an alertview with NSXMLParserErrorDomain error 5
NSLog(@"String: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //returns null
NSLog(@"Dictionary: %@",xmlDictionary); //returns null
NSMutableDictionary *tempDictForAddDate = [[NSMutableDictionary alloc] initWithDictionary:xmlDictionary];
NSDateFormatter *originalDate = [[NSDateFormatter alloc] init];
[originalDate setDateFormat:@"dd.MM.yyyy"];
NSString *today = [originalDate stringFromDate:[NSDate date]];
[tempDictForAddDate setObject:today forKey:@"updated"];
[tempDictForAddDate writeToFile:filePath atomically:YES];
self.contentList = [[tempDictForAddDate objectForKey:@"xmlObject"] objectForKey:@"event"];
[self sortContent];
}
XML 文件在我的浏览器中工作.每个标签都是关闭的.没有任何错误,但我从来没有得到文件的数据.
The XML-File works in my browser. And every tag is closed. There aren't any errors but I never get the data of the file.
希望有人能帮忙.
mavrick3.
推荐答案
您(明智地)使用了异步 url 连接,但这意味着您的 didReceiveData 委托将在数据传入时被多次调用,因此它不会在您解析它时完成.
You are (wisely) using asynchronous url connection, but this means your didReceiveData delegate will be called multiple times as the data comes in, so it won't be complete at the point you are parsing it.
您可能希望将解析移动到以下委托方法中.
You probably want to move the parsing into the following delegate method.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
请参阅 Apple 的文档这里
正式验证您的 XML 总是一个好主意 - 我倾向于使用 w3c 工具 http://www.w3schools.com/xml/xml_validator.asp
Always a good idea to formally validate your XML - I tend to use the w3c tools http://www.w3schools.com/xml/xml_validator.asp
此外,当过去工作的东西停止工作时,我总是问自己发生了什么变化?文件不一样?它更大吗?您确定它存在于服务器上并且您的浏览器没有使用缓存版本吗?
Also, when things that used to work stop working, I always ask myself what has changed? Is the file different? Is it larger? Are you sure it is present on the server and your browser isn't using a cached version?
这篇关于XML 解析 - NSXMLParserErrorDomain 错误 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!