Question

I was parsing a webService response (XML) on my app, I realized that not all the answer was being parsed (I expected at least 3 more blocks of the same info), so I decided to modify my method connectionDidFinishLoading like this:

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *XMLResponse = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSLog(@"xml message: %@, data length: %i", XMLResponse, [webData length]);

    if ([XMLResponse hasPrefix:@"[ERR]"]){
        [[[UIAlertView alloc]initWithTitle:@"Notification"
                                   message:XMLResponse != nil ? [@"there was an error while trying to load service" stringByAppendingString:XMLResponse]: @"there was an error while trying to load service"
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil]show];
        _transacWebServCompleto = @"FALSE";
        return;
    }

    xmlParser = [[NSXMLParser alloc] initWithData:webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}

at the time I run my app, I get this in output section

xml message: (null), data length: 5516

the table is printed correctly but not completely and my XML is showed as null but it contains data because of data length, any help I'll appreciate

thanks in advance

Was it helpful?

Solution

your XML may contain special characters like accented words, try with this:

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *XMLResponse = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSISOLatin1StringEncoding];

    NSLog(@"xml message: %@, data length: %i", XMLResponse, [webData length]);

    if ([XMLResponse hasPrefix:@"[ERR]"]){
        [[[UIAlertView alloc]initWithTitle:@"Notification"
                                   message:XMLResponse != nil ? [@"there was an error while trying to load service" stringByAppendingString:XMLResponse]: @"there was an error while trying to load service"
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil]show];
        _transacWebServCompleto = @"FALSE";
        return;
    }

    xmlParser = [[NSXMLParser alloc] initWithData:[XMLResponse dataUsingEncoding:NSUTF8StringEncoding]];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}

All I did was:

  1. take your NSString (XMLResponse) and change the encoding to NSISOLatin1StringEncoding
  2. initialize your NSXMLParser object (parse) by creating in one step an NSData object using your NSString (XMLResponse) but now encoding with NSUTF8StringEncoding

try it and tell us how did it go!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top