Bitnock / BNRSSFeedParser

Simple RSS and podcast parsing in Objective-C
https://github.com/Bitnock/BNRSSFeedParser
MIT License
22 stars 3 forks source link

Error parsing #2

Open alfaro28 opened 10 years ago

alfaro28 commented 10 years ago

When I try to parse a feed with white spaces at the end of the file it fails with NSXMLParserErrorMessage=Extra content at the end of the document the weird thing is sometimes it works but most of the time it fails, even weirder is that if I replace the network code with AFNetworking it will always work, any idea on why is this happening? It may be inconvenient to "tie" this project with AFNetworking but maybe it can incorporate the logic that is preventing the error in AFNetworking.

In the meanwhile replacing

NSURLSessionDataTask* task = [BNRSSFeedURLSessionManager.sharedManager.session dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
    if ([response isKindOfClass:NSHTTPURLResponse.class]) {
      self.operationResponse = (NSHTTPURLResponse*)response;

      if (error && failure) {
        failure(self.operationResponse, error);
      } else if (self.operationResponse.statusCode == 304 && success) {
        failure(self.operationResponse, error);
      } else if (data) {
        self.successBlock = success;
        self.failureBlock = failure;
        //  NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        NSXMLParser* XMLParser = [[NSXMLParser alloc] initWithData:data];
        XMLParser.delegate = self;
        [XMLParser parse];
      } else {
        failure(self.operationResponse, error);
      }
    } else {
      if (failure) {
        failure(nil, error);
      }
    }
  }];

  [task resume];

with something like

AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] init];

client.responseSerializer = [[AFXMLParserResponseSerializer alloc] init];
client.responseSerializer.acceptableContentTypes  = [NSSet setWithObjects:@"application/xml",
                                                      @"text/xml",
                                                      @"application/rss+xml",
                                                      @"application/atom+xml",
                                                      nil];
[client GET:feedURL.absoluteString
      parameters:nil
         success:^(NSURLSessionDataTask *task, NSXMLParser *responseObject) {
            self.successBlock = success;
            self.failureBlock = failure;
             [responseObject setDelegate:self];
             [responseObject parse];

         } failure:^(NSURLSessionDataTask *task, NSError *error) {
             if (failure) {
                 failure(nil, error);
             }
         }];

fixes the issue, you can try to reproduce it with the following feed http://kite-ideas.com/feed.XML

farski commented 10 years ago

This started happening in iOS 8 and I'm not sure why. There was another bug in early versions of iOS 8 that passing NSData from a data task to the XMLParser would crash; the only way to get around that was to re-encode the data several times before passing it to the parser. They seem to have fixed that problem, but this issue with Extra content... still crops up occasionally. I'm probably not going make AFNetworking a dependency for this library, so it'd be worth filing a bug report with Apple. From my testing I've seen this behavior when parsing XML even outside of this library, so I don't believe it's specific to the library's business logic.

alfaro28 commented 10 years ago

Any ideas on why this bug is not happening with AFNetworking?

farski commented 10 years ago

Not sure. I haven't used AFNetworking much since NSURLSession came out, but it probably wouldn't be hard to figure out what they're doing differently than a basic setup like this if you wanted to take a look.

Gurpartap commented 10 years ago

I don't use BNRSSFeedParser, but I've been experiencing NSXMLParserErrorMessage=Extra content at the end of the document on a simple document fetched with NSURLSession and parsed with https://github.com/tadija/AEXML which is a simple wrapper around NSXMLParser.

I looped the parser and it seems that it does work in 2nd or so attempt on the same XML data, which, along with @farski's comments above, makes me believe that this is a bug with NSXMLParser.

Gurpartap commented 10 years ago

Strangely enough, re-encoding the data (as suggested by @farski) still works. It fixed the erroneous parsing behaviour for me.

let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)!
let reEncodedData = dataString.dataUsingEncoding(NSUTF8StringEncoding)

Regarding why it does not happen when using AFNetworking, I wonder if it uses NSXMLParser any differently? Or maybe the data provided is re-encoded here as well?

ADeCraemer commented 9 years ago

Thnx Gurpartap. Encoding and Reencoding solved this error for me.