Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

XML only parsing last event

Hi Amit, I have followed your API Access course as I need to parse information from an xml file. However when the xml parses it only displays the contents of the last event tag. Any ideas?

I can post my code once I've figured out how.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Could you please paste your code because it would help to see it.

Thanks for your reply Amit.

Here's the code for the problem child.

#import "DailyAlterations.h"

@interface DailyAlterations ()

@end

@implementation DailyAlterations

@synthesize xmlParser = _xmlParser;
@synthesize currentEvent = _currentEvent;
@synthesize currentString = _currentString;
@synthesize storeCharacters = _storeCharacters;
@synthesize eventsArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {


    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gkennedy.co.uk/DailySheets.xml"]];

    self.xmlParser.delegate = self;

    self.eventsArray = [NSMutableArray array];
    self.currentString = [NSMutableString string];

    if ([self.xmlParser parse])
        NSLog(@"XML Parsed");
    else
        NSLog(@"failed to parse");

    self.title = @"Daily Alterations";



   }



#pragma mark -
#pragma  mark Parser Delegate

static NSString *kName_name = @"Name";
static NSString *kName_date = @"Date";


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"Day"]){
        self.currentEvent = [[DailyList alloc] init];
    self.storeCharacters = NO;

    }else if ([elementName isEqualToString:kName_name] || [elementName isEqualToString:kName_date]) {
        [self.currentString setString:@""];
        self.storeCharacters = YES;


    }

}


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if (self.storeCharacters) [self.currentString appendString:string];

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:kName_name]) {
        self.currentEvent.name = _currentString;

    }else if ([elementName isEqualToString:kName_date]) {
        self.currentEvent.date = _currentString;

    }if ([elementName isEqualToString:kName_date]) {
        [self finishedCurrentEvent:_currentEvent];

    }

    self.storeCharacters = NO;

}

-(void)finishedCurrentEvent:(DailyList *)e{
    [self.eventsArray addObject:e];
    self.currentEvent = nil;

}

-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{



}

-(void)parserDidEndDocument:(NSXMLParser *)parser{

    NSLog(@"%d",self.eventsArray.count);
    [self.tableView reloadData];

}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.eventsArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    float result = 44;

    return result;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    DailyList *event = [self.eventsArray objectAtIndex:indexPath.row];

    NSLog(@"%@", event);

    cell.textLabel.text = event.name;
    cell.detailTextLabel.text = event.date;

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

   return cell;
}


@end

And the XML file...

<?xml version="1.0" encoding="utf-8"?>
<Days>

<Day id="1">
    <Name>Sunday</Name>
    <Date>27/09/69</Date>
</Day>

<Day id="2">
    <Name>Monday</Name>
    <Date>25/09/69</Date>
</Day>

<Day id="3">
    <Name>Tuesday</Name>
    <Date>26/09/69</Date>
</Day>

<Day id="4">
    <Name>Wednesday</Name>
    <Date>27/09/69</Date>
</Day>

<Day id="5">
    <Name>Thursday</Name>
    <Date>28/09/69</Date>
</Day>

<Day id="6">
    <Name>Friday</Name>
    <Date>25/09/69</Date>
</Day>

<Day id="7">
    <Name>Saturday</Name>
    <Date>26/09/69</Date>
</Day>

</Days>

The Output window...

2013-08-14 18:39:51.046 rostersTest[4536:c07] 7
2013-08-14 18:39:51.048 rostersTest[4536:c07] XML Parsed
2013-08-14 18:39:51.050 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.052 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.052 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.053 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.054 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.055 rostersTest[4536:c07] 25/09/69 - 25/09/69
2013-08-14 18:39:51.055 rostersTest[4536:c07] 25/09/69 - 25/09/69

Thanks for your help.

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Do the name and date properties of DailyList have the attribute copy?

Thanks Amit, that was it, I must have set them at strong out of habit!

Can I just say at this point that this is what puts Team Treehouse head and shoulders above any other education site I've been on, you take the time to reply to your forum members questions.

You are a true professional and a gent.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Mistakes are the best way to learn, next time you will probably not make the same one again :) Thank you for the kind words we try to do our best to help you learn and that includes the forums.