I do the same things in my apps, I use ASIHttpRequest which works perfect. After getting my string back, i translate to a dictionary.
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:myString];
This dictionary I send to a parse class, which I can get a object from.
xmlTrainNumber* fileResult = [[[xmlTrainNumber alloc] initWithDictionary:dict] autorelease];
the class xmlTrainNumber looks like this:
#import "xmlTrainNumber.h"
#import "trainNumberResultSet.h"
@interface xmlTrainNumber (Private)
- (NSArray*)_parseXmlDictionary:(NSDictionary*)aDictionary;
@end
@implementation xmlTrainNumber
@synthesize timeXmlResult;
- (id)initWithDictionary:(NSDictionary*)aDictionary
{
self = [super init];
if (self)
{
timeXmlResult = [self _parseXmlDictionary:aDictionary];
}
return self;
}
- (NSArray*)_parseXmlDictionary:(NSDictionary*)aDictionary
{
if (aDictionary != NULL && [aDictionary count] > 0)
{
NSNumber *version = [aDictionary objectForKey:@"version"];
NSNumber *statusCode = [aDictionary objectForKey:@"statusCode"];
if ([[statusCode stringValue] isEqualToString:@"1"])
{
NSString *title = [aDictionary objectForKey:@"title"];
if (version != NULL)
{
NSArray* results = [aDictionary objectForKey:@"results"];
if (results != NULL)
{
NSMutableArray* result = [[NSMutableArray alloc] init];
for (NSDictionary* currentResult in results)
{
// TODO: add error handling
[result addObject:[[trainNumberResultSet alloc] initWithStation:[currentResult objectForKey:@"station"]
arrival:[currentResult objectForKey:@"arrival"]
departure:[currentResult objectForKey:@"departure"]
newArrival:[currentResult objectForKey:@"newArrival"]
newDeparture:[currentResult objectForKey:@"newDeparture"]
expectedArrival:[currentResult objectForKey:@"expectedArrival"]
expectedDeparture:[currentResult objectForKey:@"expectedDeparture"]
track:[currentResult objectForKey:@"track"]
info:[currentResult objectForKey:@"info"]
title:title]];
}
return result;
}
else
{
// TODO: throw exception instead
return NULL;
}
}
else
{
// TODO: throw exception instead
return NULL;
}
}
else {
return nil;
}
}
else
{
// TODO: throw exception instead
return NULL;
}
}
- (NSArray*)getTimeResult
{
return timeXmlResult;
}
- (void)dealloc
{
if (timeXmlResult != NULL)
{
[timeXmlResult release];
}
[super dealloc];
}
@end
The class trainNumberResultSet
is just a class with some setters that saves the data assigned. I have some todo
's left in this code... but I hope that this can help you anyways. It works for me. The array is a list of trainNumberResultSet
objects.
Regards,
Paul Peelen