NSXML didStartElement method not being called

1k views Asked by At

Iv'e set my h as the delegate and I'm calling the "setDelegate:self" method on my xml parser. here is my code.

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    elementName = element;
    if ([elementName isEqualToString:@"offer"]) {
        offersDictionary = [[NSMutableDictionary alloc] init];
        offerTitle = [[NSMutableString alloc] init];
        offerDay = [[NSMutableString alloc] init];
        offerDet = [[NSMutableString alloc] init];
        NSLog(@"PARSER didStartElement method!");
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if ([element isEqualToString:@"offerTitle"]) {
        [offerTitle appendString:string];
    }
    if ([element isEqualToString:@"day"]) {
        [offerDay appendString:string];
    }
    if ([element isEqualToString:@"offerDetail"]) {
        [offerDet appendString:string];
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"offer"]) {
        [offersDictionary setObject:offerTitle forKey:@"offerTitle"]; NSLog(@"offerTitle:%@",offerTitle);
        [offersDictionary setObject:offerDet forKey:@"offerDetail"]; NSLog(@"offerDet:%@",offerDet);
        [offersDictionary setObject:offerDay forKey:@"day"]; NSLog(@"OfferDay:%@",offerDay);
        NSLog(@"DidEndELEMENT");

        //[offersArray addObject:[offersDictionary copy]];
    }

}

I've set "NSlog" in my didStartElement and in my didEndElement methods, but I only get output from the didEndElement.

I'm calling the "parse" method of the NSXML from the viewDidLoad method.

here is my viewDidLoad method.

- (void)viewDidLoad
{
    [super viewDidLoad];
    //alloc and init the array
    offersArray = [[NSMutableArray alloc] init];
    //create NSUrl with the xml file
    NSURL *xmlUrl = [NSURL URLWithString:@"http://mydomain.org/dir/dir/file.xml"];
    //alloc and init the xml parser with the data of the file
    xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlUrl];
    //set some methods on the xml parser
    [xmlParser setDelegate:self];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];
    //create a 'success gate' of the 'parse' method
    BOOL xmlParseSuccess = [xmlParser parse];
    if (xmlParseSuccess) {
        NSLog(@"Successed Parsing! array Has %lu elements.", (unsigned long)offersArray.count);
    } else if (!xmlParseSuccess) {
        NSLog(@"Error Parsing!");
    }

    // Do any additional setup after loading the view.
}

Why is the didStartElement method not being called?

1

There are 1 answers

1
random On BEST ANSWER

Here is your issue:

-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
   attributes:(NSDictionary *)attributeDict {

    /*
     Your setting elementName to element. So whatever the
     value of 'element' is, it will compare to @"offer". 
     It seems that the value of 'element' is not equal 
     to @"offer". You should not be Comment this line out 
     and it should work fine.
     */

    elementName = element;

    if ([elementName isEqualToString:@"offer"]) {
        offersDictionary = [[NSMutableDictionary alloc] init];
        offerTitle = [[NSMutableString alloc] init];
        offerDay = [[NSMutableString alloc] init];
        offerDet = [[NSMutableString alloc] init];
        NSLog(@"PARSER didStartElement method!");
    }
}