views:

27

answers:

1

Hi all,

I am really struggling on this. I have an XML feed:

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
    <date>
        <name>27/9/10</name>
        <event>
            <title>Event 1</title>
            <more>Copy in here</more>
        </event>
    </date>
    <date>
        <name>04/10/10</name>
        <event>
            <title>Event 1</title>
            <more>Copy in here</more>
        </event>
        <event>
            <title>Second Event</title>
            <more>Copy in here</more>
        </event>
    </date>
</data>

I can manage to parse the xml with:

- (void)parseXMLFileAtURL:(NSString *)URL {
    sections = [[NSMutableArray alloc] init];
    items = [[NSMutableArray alloc] init];

    NSURL *xmlURL = [NSURL URLWithString:URL];
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [rssParser setDelegate:self];
    [rssParser parse];
}

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

}

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

    currentElement = [elementName copy];

    if ([elementName isEqualToString:@"event"]) {
        currentTitle = [[NSMutableString alloc] init];
        currentSummary = [[NSMutableString alloc] init];
    }

    if ([elementName isEqualToString:@"date"]) {
        item = [[NSMutableDictionary alloc] init];
    currentSection = [[NSMutableString alloc] init];
    }
}

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

    if ([elementName isEqualToString:@"date"]) {
        [item setObject:[NSNumber numberWithInt:itemsCount] forKey:@"itemsCount"];
        [item setObject:currentSection forKey:@"name"];
        [items addObject:[item copy]];

        itemsCount = 0;
    }

    if ([elementName isEqualToString:@"event"]) {
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentSummary forKey:@"more"];
        [items addObject:[item copy]];

        itemsCount = itemsCount + 1;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"name"]) {
        [currentSection appendString:string];
    } else if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];
    } else if ([currentElement isEqualToString:@"more"]) {
        [currentSummary appendString:string];
    }
}

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

    [self.progressView.view removeFromSuperview];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [dataTable reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([sections count] == 0) {
        NSString * path = @"http://www.campuscloud.co.uk/_dev/calendar.xml";
        [self parseXMLFileAtURL:path];
    }

}

But when I try and display this in my table it all goes wrong when I display more than two events for the one date. I want the date to show as a section header and the events to be displayed below.

Any help on this welcome...

+1  A: 

Hey buddy.. I don't know if I understood coorect, but let's go..

You can create a dictionary of arrays..

NSMutableDictionary *dates = [NSMutableDictionary alloc];
NSMutableArray *events = [NSMutableArray alloc];

in the array you will insert all event of one date..

[events addObject:@"event 1"];

in the dictionary you will insert the array of events with tha date as key

[dates setobject:events forKey:@"01/01/2010"];

whenever the parser find a new date, a new array should be created (I mean, the values are new.. don't append the array, clean it.. but you don't need to create a brand new array)

of course that you have to make some changes on the parser to identify the dates and etc, but it's not a big deal.

in your table each section is one key from your dictionary.. so the number of sections is the number of items on the dictionary.. and the values inside the section is the array of each date..

I hope it helps..

Roberto Ferraz de Novaes