RestKit从本地存储的XML解析/对象映射

时间:2013-11-28 00:42:51

标签: ios iphone ipad restkit

我正在下载一个zip,解压缩,然后尝试从zip中包含的xml文件解析和对象地图数据。我知道如果XML文件不是拉链,我可以做像

这样的事情
NSURL *URL = [NSURL URLWithString:@"http://restkit.org/articles"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

但是因为我下载了一个zip,我必须(或者至少我认为我必须)使用以下代码

NSURL *URL = [NSURL URLWithString:@"http://www.mediafire.com/download/6tfd33xkiepx8a3/db_UI.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
self.operation = [[AFHTTPRequestOperation alloc]
                  initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"testDownload"];
self.operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

// Set download completion block
[self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Successfully downloaded file to %@", path);
     NSString *zipPath = path;
     NSString *destinationPath = [paths objectAtIndex:0];
     [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath delegate:self];

 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     // NSLog(@"error: %@",  operation.responseString);
 }];


// Start download operation
[self.operation start];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];

所以在这种情况下,我该如何使用RKObjectMapping。或者有替代方案吗?.......

1 个答案:

答案 0 :(得分:0)

这是来自Rest-Kit docs,这个解析JSON对象,如果你可以改变你可以用XML做什么的MIME类型

您可以在此处找到它:Rest-Kit docs

NSString* JSONString = @"{ \"name\": \"The name\", \"number\": 12345}";
NSString* MIMEType = @"application/json";
NSError* error = nil;
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
id parsedData = [parser objectFromString:JSONString error:&error];
if (parsedData == nil && error) {
    // Parser error...
}


RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider;
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
RKObjectMappingResult* result = [mapper performMapping];
if (result) {
    // Yay! Mapping finished successfully
}