2013-12-16 3 views
-1

Iv'e устанавливает мой h в качестве делегата, и я вызываю метод setDelegate: self на моем XML-синтаксисе. вот мой код.NSXML didStartElement метод, который не называется

-(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]]; 
    } 

} 

Я установил «NSLog» в моем didStartElement и в моих методах didEndElement, но я только получить выход из didEndElement.

Я вызываю метод «разбора» NSXML из метода viewDidLoad.

вот мой метод просмотраDidLoad.

- (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. 
} 

Почему метод didStartElement не называется?

ответ

0

Вот ваш вопрос:

-(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!"); 
    } 
} 
+0

Iv'e нашел мою проблему с you'r помочь! необходимо изменить 'elementName = element;' на 'element = elementName'. Спасибо! – Ompel