RestKit XML Mapping具有相同的元素和属性名称

时间:2013-12-07 00:29:43

标签: xml xml-serialization restkit rkobjectmapping

我目前正在使用带有RKTBXMLSerialization的RestKit与基于xml的宁静Web服务进行通信。我有一个非常特殊的问题,对象映射一个调用的响应。这个特殊元素有一个与节点本身同名的属性,比方说:

<List ListName="List1">
  <Item Name="ItemName1" Item="ItemValue1" />
  <Item Name="ItemName2" Item="ItemValue2" />
</List>

我对应的Mapping看起来像这样:

RKObjectMapping *listMapping = [RKObjectMapping mappingForClass:[ListModel class]];
[listMapping addAttributeMappingsFromArray:@[@"ListName"]];

RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[ItemModel class]];
[itemMapping addAttributeMappingsFromArray:@[@"Name", @"Item"]];

RKRelationshipMapping *itemRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Item" toKeyPath:@"Items" withMapping:itemMapping];

[listMapping addPropertyMapping:itemRelationship];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"List" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:@[responseDescriptor]];

使用这些模型类:

@interface ListModel : NSObject
  @property (nonatomic, strong) NSArray* Items;
  @property (nonatomic, copy) NSString* ListName;
@end

@interface ItemModel : NSObject
  @property (nonatomic, copy) NSString* Name;
  @property (nonatomic, copy) NSString* Item;
@end

我得到的错误信息是:

'NSUnknownKeyException', reason: '[<__NSCFString 0xb816420> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Item.'

我已经尝试了很多,并且可以将问题跟踪到xml结构中的重复名称“Item”。由于我无法影响响应模式,因此我需要一个客户端解决方案。

问题也可能在RKTBXMLSerialization(TBXML)中,所以我将尝试RKXMLReaderSerialization。有没有人有这个问题甚至是解决方案?

1 个答案:

答案 0 :(得分:0)

正如我已经发布的那样,我现在也尝试了RKXMLReaderSerialization,问题就消失了。代码解决方案正在改变

[RKMIMETypeSerialization registerClass:[RKTBXMLSerialization class] forMIMEType:@"application/xml"];

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];

我将在GitHub上发布RKTBXMLSerialization的问题,所以也许它将在未来的版本中修复。