2

I am trying to parse an xml feed on the app delegate didFinishLaunchingWithOptions: method:

//Parse XML Data
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://xxxxxxxxxxxxxxxxx/xml"]];//url is just fine :)
    [xmlParser setDelegate:self];
    BOOL parseState = [xmlParser parse];
    if (parseState) {
        NSLog(@"parse succeeded");
    }else{
        NSLog(@"parse failed");//Always parse failed, parse is always "NO"
    }

Delegate protocol methods (none is called):

#pragma mark - NSXMLParserDelegate

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    NSLog(@"parser:didStartElement:namespaceURI:qualifiedName:attributes:");

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    NSLog(@"parser:foundCharacters:");
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"parser:didEndElement:namespaceURI:qualifiedName:");


}

And of course, AppDelegate.h conforms to the NSXMLParserDelegate protocol:

@interface AppDelegate : UIResponder <UIApplicationDelegate,NSXMLParserDelegate>

What am I missing? why protocol methods are not called and why [xmlParser parse] always return NO? Thanx.

Malloc
  • 15,434
  • 34
  • 105
  • 192
  • 1
    Implement the `parser:parseErrorOccurred:` delegate method and see what the error is. – rmaddy Jul 20 '13 at 02:08
  • Hi rmaddy, I used that delegate method, the parameter `parseError`'s code is `5` and the dictionary is just empty. – Malloc Jul 20 '13 at 02:42
  • Are you 100% certain the XML document is well-formed? An error code 5 while parsing XML is `NSXMLParserPrematureDocumentEndError`. Can you create an `NSURLConnection` to download the file from the same URL using `sendSynchronousRequest:`, and print the response to the console? – Craig Otis Jul 20 '13 at 03:09

2 Answers2

0

Is there a reason you're cramming your XML parser inside the AppDelegate? Without exception I've found it to be beneficial to always instantiate a parser class to handle parsing tasks. For example...

myParser.h

@class parseClass;

@interface myXMLParser : NSObject <NSXMLParserDelegate> {

}

- (myXMLParser *) initmyXMLParser;

myParser.m

//I do my instantiating like this so it can only be done once
//If you need to instantiate the class more than once do NOT use dispatch_once!
- (myXMLParser *)initmyXMLParser
{
    static myXMLParser *newInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        newInstance = [[myXMLParser alloc]init];
    });
    return newInstance;
}
//include other delegate methods as well

AppDelegate.m

#import "myParser.h"

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL      URLWithString:@"http://xxxxxxxxxxxxxxxxx/xml"]];//url is just fine :)
myXMLParser *theParser = [[myXMLParser alloc]initmyUpdateXMLParser];
[xmlParser setDelegate:theParser];
BOOL parseState = [xmlParser parse];
if (parseState) {
    NSLog(@"parse succeeded");
}else{
    NSLog(@"parse failed");//Always parse failed, parse is always "NO"
}
Dan
  • 5,153
  • 4
  • 31
  • 42
  • That code is stripped down and refactored from a project I have open right now... and meshed with your example above. So, yes, the basis & logic behind the code works. Did I create a typo somewhere doing this from the hip? Possibly. – Dan Jul 20 '13 at 02:40
0

Might it be an ARC error, try setting up xmlparser as a strong property so that it won't be deallocated before finishing parsing the file.

You may want to download the xml from the web before parsing it using an nsurlconnection

Blevz
  • 30
  • 5
  • I bet about it before going to sleep yesterday and first thing I tried when I wake up this morning. That was exactly the problem, I had to make the `NSXMLParser`a strong property (`retain` for pre-ARC terminology). – Malloc Jul 20 '13 at 07:24