-1

我将我的 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 的“解析”方法。

这是我的 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 方法?

4

1 回答 1

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!");
    }
}
于 2013-12-16T23:28:09.920 回答