RestKit XML Mapping与嵌套元素的问题

时间:2015-03-03 22:48:39

标签: xml restkit response

我正在使用RestKit .22,并且在将XML响应映射到实体方面存在问题。一切正常,我得到200OK,但对象没有正确映射。在XML响应中,有一个根元素“响应”,在它下面是一个“事件”数组。

<response>
  <meta>...</meta>
  <events>
      <event>...// data that I'm after reflected in the entity. </event>
  </events>
</response>

我的代码:

- (void)setupRestKitWithBaseURL:(NSURL *)baseURL{
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeXML];
[objectManager setAcceptHeaderWithMIMEType:@"application/xml"];

// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;

NSEntityDescription *entity = [[managedObjectStore.managedObjectModel entitiesByName] objectForKey:@"Event"];
RKEntityMapping *eventMapping = [[RKEntityMapping alloc] initWithEntity:entity];
[eventMapping addAttributeMappingsFromArray:@[ @"title" ]];
[eventMapping addAttributeMappingsFromDictionary:@{
                                                   @"id" : @"remoteID",
                                                   @"date_tbd" : @"dateToBeDecided",
                                                   @"time_tbd" : @"timeToBeDecided",
                                                   @"datetime_utc" : @"start",
                                                   @"venue.name" : @"location"
                                                   }];

RKResponseDescriptor *eventIndexResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping method:RKRequestMethodGET pathPattern:@"events" keyPath:@"response" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:eventIndexResponseDescriptor];


[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"events"];
    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    if (match) {
        return [NSFetchRequest fetchRequestWithEntityName:@"Event"];
    }
    return nil;
}];

[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Example.sqlite"];
[managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

}

控制台:

[63994:2621733] I restkit.network:RKObjectRequestOperation.m:220 GET 'http://api.seatgeek.com/2/events?format=xml&lat=39.75&lon=-104.984&per_page=500&range=10mi&taxonomies.name=sports' (200 OK / 2 objects) [request=0.0002s mapping=0.0018s total=0.0035s]

所以我加载了2个事件,它们是“response”元素的嵌套元素:“meta”&amp; “事件”,但不是“事件”数组下的实际事件,这是我需要的。非常感谢。 感谢

更新

除了Wain的回答,我还将这些值更改为value.text:

[eventMapping addAttributeMappingsFromDictionary:@{
                                                   @"title.text" : @"title",
                                                   @"id.text" : @"remoteID",
                                                   @"date_tbd.text" : @"dateToBeDecided",
                                                   @"time_tbd.text" : @"timeToBeDecided",
                                                   @"datetime_utc.text" : @"start",
                                                   @"venue.name.text" : @"location"
                                                   }];

1 个答案:

答案 0 :(得分:1)

您需要指定深入到映射级别的完整密钥路径。根据您的说明,它应该是response.events.event,但您刚刚设置了response