开发者

XML Parsing - NSXMLParserErrorDomain error 5

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 delega开发者_如何学Gote: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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜