适用于iOS的XML Parser问题

时间:2011-03-06 04:23:21

标签: xml ios xml-parsing

嘿伙计们(和女士们),我目前正在开发一个相对简单的iPhone应用程序,它可以从数据库中提取记录。

我正在尝试使用NSXMLParser来实现它,但出于某种原因,在达到第10个元素后,整个应用程序决定启动并退出。我不太清楚为什么会这样。日志没有给出任何错误。它就此停止。

我已经包含了我们已经获得的didStartElement和didEndElements。

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

和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"]){
        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]);

}

同样,任何和所有的建议都会非常感激。

1 个答案:

答案 0 :(得分:0)

我建议使用TouchXML

它非常易于使用,并没有给我带来任何问题。