views:

400

answers:

2

I will try to explain shortly what my problem is...

I'm loading an XML file (using touchXML) to populate a UITableView with data.

expositions = [[NSMutableArray alloc] init];

// Get XML string from XML document.
NSString *xmlURL = EXPOSITION_XML_DOC;
NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:xmlURL]];
NSString *xmlStringUTF8 = [NSString stringWithUTF8String:[xmlString UTF8String]];
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:xmlStringUTF8 options:0 error:nil];

// Parse XML document.
NSArray *expos = [xmlDoc  nodesForXPath:@"/expositions/exposition" error:nil];

if (expos != nil && [expos count] >= 1)
{
    // create Exposition instances with data from xml file
    for (CXMLElement *expo in expos)
    {
        NSString *date = [[[[expo attributeForName:@"date"] stringValue] copy] autorelease];
        NSString *name = [[[[expo attributeForName:@"name"] stringValue] copy] autorelease];
        NSString *place = [[[[expo attributeForName:@"place"] stringValue] copy] autorelease];
        NSLog(@"hello , %@, %@, %@", date, name, place);

        Exposition *e = [[Exposition alloc] initWithDate:date name:name place:place];
        [expositions addObject:e];
        NSLog(@"%@", e.name);
    }
}

At the end of the for loop, you can see that output some of the data just passed and it works fine in NSLog returning the parsed XML data.

But when I try to access the array in

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

like so...

// get exposition data for this row
    Exposition *e = [expositions objectAtIndex:indexPath.row];

    NSString *str = e.name;
    NSLog(@"%@", str);

I get an error! And I don't understand why. I have some German umlauts in the parsed XML data but I defined the UTF-8 encoding so this should not be a problem. But if it is, please tell me a fix. Or whatever else could be the problem...

A: 

I think you are redeclaring theExposition *e variable in cellForRowAtIndexPath method. Declare that variable in .h file.

Ruchir Shah
tried it but did not work. Honestly I also don't see why it should not work when declaring this variable inside the cellForRowAtIndexPath. I just can not get the data out of this instance this seems to crash the app. Why?
jd291
modify the line as follows, if you are writing this line in cellForRowAtIndexPath method:NSString *str = [[e objectAtIndex:i] name];
Ruchir Shah
A: 

I just used NSXMLParser now and everything works fine, even with umlauts.

Since the XML file is going to be of small file size anyway, the performance in contrast to the touchXML library should not matter that much.

Thx to the people that tried to help.

jd291