开发者

Problems w/XML Parser for iOS

Hey guys (and gals), I'm currently working on a relatively simple iPhone app which pulls records from a database.

I'm trying to use NSXMLParser to do it, but for some reason, after reaching the 10th element, the entire app just decides to up and quit. I'm not quite sure why this happens. We aren't given any errors by the log. It just stops.

I've included the didStartElement and didEndElements that we've got.

didStartelement

- (void)parser:(NSXMLParser *)parser didStartElement:(NSMutableString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//NSLog(@"elementName = %@", elementName);
txt = [[NSMutableArray alloc] init];
//curElem = elementName;
//currentRestaurant = [[Restaurant alloc] init];


if ( [elementName isEqualToString:@"Name"]){
    curElem = elementName;
    currentRestaurant = [[Restaurant alloc] init];

    return;
}

else {
    curElem = nil;
}

and didEndElement

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//if( [elementName isEqualToString:@"Name"] ) {
    // Do something here
    //NSLog(@"TXT: %@", txt);

    if([elementName isEqualToString:@"RestID"]){
        currentRestaurant.RestID = [txt integerValue];
        NSLog(@"RestID: %@", currentRestaurant.RestID);

    }

    if ([elementName isEqualToString:@"Name"]) {
        currentRestaurant.Name = txt;
        NSLog(@"Name: %@", currentRestaurant.Name);
    }

    else if([elementName isEqualToString:@"Address"]){
        currentRestaurant.Address = txt;
        NSLog(@"Address: %@", currentRestaurant.Address);

    }
    else if([elementName isEqualToString:@"Phone"]){
        currentRestaurant.Phone = txt;
        NSLog(@"Phone: %@", currentRestaurant.Phone);

    }
    else if([elementName isEqualToString:@"NumNoms"]){
        currentRestaurant.NumNoms = [txt integerValue];
        NSLog(@"NumNoms: %@", currentRestaurant.NumNoms);

    }
    else if([elementName isEqualToString:@"PriceID"]){
        currentRestaurant.PriceID = [txt integerValue];
        NSLog(@"PriceID: %@", [NSString stringWithFormat:@"%d", currentRestaurant.PriceID]);

    }
    else if([elementName isEqualToString:@"OpenTime"]){
        currentRestaurant.OpenTime = txt;
        NSLog(@"OpenTime: %@", currentRestaurant.OpenTime);

    }
    else if([elementName isEqualToString:@"CloseTime"]){
        currentRestaurant.CloseTime = txt;
        NSLog(@"CloseTime: %@", currentRestaurant.CloseTime);

    }
    else if([elementName isEqualToString:@"TypeID"]){
        currentRestaurant.OpenTime = txt;
        NSLog(@"TypeID: %@", currentRestaurant.TypeID);

    }
    else if([elementName isEqualToString:@"imgURL"]){
       开发者_如何学C currentRestaurant.imgURL = txt;
        NSLog(@"imgURL: %@", currentRestaurant.imgURL);

    }
    else if([elementName isEqualToString:@"Coupons"]){
        //currentRestaurant.Coupons = txt;
        //NSLog(@"Coupons: %@", currentRestaurant.Coupons);


    }
    else if([elementName isEqualToString:@"Specials"]){
        currentRestaurant.Specials = [NSString stringWithFormat:@"%d", [txt integerValue]];
        NSLog(@"Specials: %@", currentRestaurant.Specials);

    }
    else if([elementName isEqualToString:@"Flagged"]){
        currentRestaurant.Flagged = [NSString stringWithFormat:@"%d", [txt integerValue]];
        NSLog(@"Flagged: %@", currentRestaurant.Flagged);

    }
    else if([elementName isEqualToString:@"timesFlagged"]){
        currentRestaurant.timesFlagged = [txt integerValue];
        NSLog(@"timesFlagged: %@", currentRestaurant.timesFlagged);

    }
    else if([elementName isEqualToString:@"Delivery"]){
        currentRestaurant.Delivery = [NSString stringWithFormat:@"%d", [txt integerValue]];
        NSLog(@"Delivery: %@", currentRestaurant.Delivery);

    }
    else if([elementName isEqualToString:@"TigerBucks"]){
        currentRestaurant.TigerBucks = [NSString stringWithFormat:@"%d", [txt integerValue]];
        NSLog(@"TigerBucks: %@", currentRestaurant.TigerBucks);
        [xmlData addObject:currentRestaurant];
        [currentRestaurant dealloc];
    }


    //NSLog(@"Current Rest name: %@", currentRestaurant.Name);
    //[xmlData addObject:currentRestaurant];
    for (id obj in xmlData)
        NSLog(@"A record in xmlData: %@", obj);
    [objArray addObject:txt];
    //NSLog(@"objArray: %@", [objArray count]);
    //NSLog(@"xmlData: %@", [xmlData count]);
    [txt release];

    return;
//}
[txt release];
txt = nil;
//NSLog(@"objArray: %@", [objArray count]);
//NSLog(@"xmlData: %@", [xmlData count]);

}

Again, any and all advice would be really appreciated.


I would recommend using TouchXML.

It is very easy to use and has not caused me any problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜