我做我的應用程序同樣的事情,我用ASIHttpRequest這完美的作品。在得到我的字符串後,我翻譯成字典。
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:myString];
這個字典我發送到一個解析類,我可以從中得到一個對象。
xmlTrainNumber* fileResult = [[[xmlTrainNumber alloc] initWithDictionary:dict] autorelease];
類xmlTrainNumber看起來是這樣的:
#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
類trainNumberResultSet
只是一些制定者,節省了分配的數據類。我在這段代碼中留下了一些todo
......但我希望這可以幫助你。這個對我有用。該數組是一個trainNumberResultSet
對象的列表。