delegate methods are not called and parse property is always FALSE

2.1k views Asked by At

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.

2

There are 2 answers

1
Blevz On BEST ANSWER

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

1
Dan On

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"
}