XML解析不显示TableView上的数据

时间:2013-08-15 11:22:03

标签: ios parsing nsxmlparser

似乎我无法在xml内的表格中显示数据...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

            }

    CGRect nameFrame = CGRectMake(45, 2, 265, 30);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
    nameLabel.numberOfLines = 2;
    nameLabel.font = [UIFont boldSystemFontOfSize:12];
    nameLabel.text = [currentData nameOfCat];
    [cell.contentView addSubview:nameLabel];

    CGRect descFrame = CGRectMake(45, 40, 265, 10);
    UILabel *descLabel = [[UILabel alloc] initWithFrame:descFrame];
    descLabel.font = [UIFont systemFontOfSize:10];
    descLabel.text = [currentData descriptionOfCat];
    [cell.contentView addSubview:descLabel];
    return cell;

}

这是xmlparser类.m文件

- (id) loadXMLByURL :(NSString *) urlString {

    listPopulated = [[NSMutableArray alloc]init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data= [[NSData alloc]initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;

    return self;

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    currentList  = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if([elementName isEqualToString:@"category"]){
        dataCurrent = [dataFileHolder alloc];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"name"]){
        dataCurrent.nameOfCat = currentList;
    }
    if ([elementName isEqualToString:@"description"]){
        dataCurrent.descriptionOfCat = currentList;
    }
    if ([elementName isEqualToString:@"category"]) {
        [listPopulated addObject:dataCurrent];
        dataCurrent = nil;
        currentList = nil;
    }
}

然后我有一个类来保存从xml解析的数据。 .m数据持有者就是这个

@implementation dataFileHolder

@synthesize nameOfCat = _nameOfCat;
@synthesize descriptionOfCat = _descriptionOfCat;

@end

这是xml数据

category>
<cat_id>59</cat_id>
<name>Foods & Meals</name>
<description>
iRabwah Foods & Meals service provides you pizza packages, burger meals and spicy rice meal. Give a best meal to your loved ones in rabwah by iRabwah
</description>
<image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/1287455832_monotone_fork_spoon_eat_launch_restaurant_dinner.png
</image>
<sub_cat>
<sub_id>67</sub_id>
<sub_name>Spicy Rice Meal</sub_name>
<sub_desc>
iRabwah offers you to give a treat to your loved ones by spicy rice meal, best taste and special rice to your loved ones in rabwah.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/hyderabadi_biryani.png
</sub_image>
</sub_cat>
<sub_cat>
<sub_id>69</sub_id>
<sub_name>Pizza Packages</sub_name>
<sub_desc>
Our pizzas have exciting, universal flavors made in Rabwah from freshly baked pizza dough including pepperoni, oregano, sauces and fresh ingredients.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/pizza.png
</sub_image>
</sub_cat>
<sub_cat>
<sub_id>68</sub_id>
<sub_name>Burger Meals</sub_name>
<sub_desc>
iRabwah provides you a best way to dedicate a wonderful meal to your loves ones in rabwah by offering burger meals with fries coke and pizza.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/burger-clip-art-4.png
</sub_image>
</sub_cat>
</category>

请告诉我我失踪了什么

1 个答案:

答案 0 :(得分:1)

您必须有一个BOOL变量才能开始捕获字符。

或尝试这样:

- (id) loadXMLByURL :(NSString *) urlString {

    listPopulated = [[NSMutableArray alloc]init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data= [[NSData alloc]initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;

    [parser parse];

    return self;
 }

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    currentList  = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if([elementName isEqualToString:@"category"]){
        dataCurrent = [dataFileHolder alloc];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"name"]){
        dataCurrent.nameOfCat = currentList;
    }
    if ([elementName isEqualToString:@"description"]){
        dataCurrent.descriptionOfCat = currentList;
    }
    if ([elementName isEqualToString:@"category"]) {
        [listPopulated addObject:dataCurrent];
        dataCurrent = nil;
        currentList = nil;
    }
}