XML Parsing - NSXMLParserErrorDomain error 5

落爺英雄遲暮 提交于 2019-12-11 17:39:36

问题


I'm trying to parse a XML File. It worked very well - until today...

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

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.

I hope someone can help.

mavrick3.


回答1:


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

See Apple's Documentation here

EDIT:

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?



来源:https://stackoverflow.com/questions/5486116/xml-parsing-nsxmlparsererrordomain-error-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!