iosnsxmlparsernsxmlparserdelegate

delegate methods are not called and parse property is always FALSE


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.


Solution

  • 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