iOS XML 解析器的问题
大家好,我目前正在开发一个相对简单的 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]);
}
再次强调,任何和所有建议都将非常感激。
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"]){
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 TouchXML。
它非常容易使用并且没有给我带来任何问题。
I would recommend using TouchXML.
It is very easy to use and has not caused me any problems.