2013-12-16 51 views
-1

Iv'e将我的h设置为委托,并在我的xml解析器上调用“setDelegate:self”方法。 这是我的代码。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得到输出。

我从viewDidLoad方法调用NSXML的“parse”方法。

这里是我的viewDidLoad方法。

- (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发现我的问题,找你帮忙!将'elementName = element;'改为'element = elementName'所需的 。 谢谢! – Ompel