Ace Munim
Ace Munim

Reputation: 325

XMLParsing not showing data on TableView

It seems that I am unable to data show in the table that is inside the 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;

}

this is the xmlparser class the .m file

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

then i have a class that will hold the data parsed from the xml. the .m dataholder is this

@implementation dataFileHolder

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

@end

and this is the xml data

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>

kindly tell me what im missing

Upvotes: 1

Views: 153

Answers (1)

Samkit Jain
Samkit Jain

Reputation: 2533

You must have a BOOL variable to start capturing characters.

or try like this :

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

Upvotes: 1

Related Questions