NSXML didStartElement方法未被调用

时间:2013-12-16 23:16:37

标签: ios objective-c nsxmlparser nsxml nsxmlparserdelegate

我将我的h设置为委托,我在我的xml解析器上调用“setDelegate:self”方法。 这是我的代码。

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

}

我在didStartElement和didEndElement方法中设置了“NSlog”,但我只从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方法?

1 个答案:

答案 0 :(得分: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!");
    }
}