2012-09-17 66 views
0

我使用RestKit從資源加載XML數據。這工作完美,但現在我想改變我的整個程序來讀取本地XML文件。我發現讀本地XML文件,並在谷歌RestKit組解析它通過RestKit可能性:使用本地XML的RestKit

NSString *string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"testing" ofType:@"xml"] encoding:NSUTF8StringEncoding error:nil]; 
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeXML]; 
id result = [parser objectFromString:string error:nil]; 
NSLog(@"%@",[[[[result objectForKey:@"sample"] objectForKey:@"nested"] objectForKey:@"tags" ] description]); 

但現在我希望加載這個對象與我的映射。我的舊代碼從服務器加載:

RKObjectManager *objectManager = [RKObjectManager sharedManager]; 
RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[xmlFile path] queryParameters:nil]; 

[objectManager.mappingProvider setMapping:myMapping forKeyPath:myKeyPath]; 

[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@", [URL resourcePath]] usingBlock:^(RKObjectLoader *loader) { 
    loader.delegate = self; 
}]; 

之後,我在RestKit的didLoadObjects委託中獲得了我的映射對象。我怎樣才能爲我的本地XML對象做同樣的事情?

回答

0

我找到了以下問題解決方案。奇蹟般有效。

RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider; 
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider]; 
RKObjectMappingResult* result = [mapper performMapping]; 
if (result) { 

    NSArray *resultArray = result.asCollection; 

    MyMappingClass *object = [resultArray lastObject]; 
    NSLog(@"My Object: %@", object); 
} 
0

對於Restkit 0.20,我用這個方法:

library.xml

<?xml version="1.0" encoding="UTF-8"?> 
<library> 
    <bookname>azeaze</bookname> 
    <bookname>qdd</bookname> 
    <bookname>vvef</bookname> 
    <bookname>ytrtefc</bookname> 
</library> 

Book.h

#import <Foundation/Foundation.h> 

@interface Book : NSObject 

@property (nonatomic,strong) NSString * label; 

@end 

Book.m

#import "Book.h" 

@implementation Book 

@end 

我用這個XML文件創建一個NSDictionary,爲此我使用XMLReader

#import "XMLReader.h" 

[...] 

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Book class]]; 
[mapping addAttributeMappingsFromDictionary:@{@"bookname": @"label",}]; 

NSString *pathToDataFile = [[NSBundle mainBundle] pathForResource:@"library" 
                  ofType:@"xml"]; 

NSError *error; 

NSString* contents = [NSString stringWithContentsOfFile:pathToDataFile 
               encoding:NSUTF8StringEncoding 
                error:&error]; 

NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding]; 

NSError *parseError = nil; 

NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:xmlData 
                 error:&parseError]; 

NSDictionary *mappingsDictionary = @{ @"library": mapping }; 

RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:xmlDictionary 
                  mappingsDictionary:mappingsDictionary]; 

NSError *mappingError = nil; 

BOOL isMapped = [mapper execute:&mappingError]; 

if (isMapped && !mappingError) { 
    NSLog(@"SUCCESS : %@",[mapper mappingResult]); 
} else { 
    NSLog(@"FAIL : %@",mappingError); 
}