![](/img/trans.png)
[英]Are there problems with using the same name for an XML element and attribute?
[英]RestKit XML Mapping with same element and attribute name
我目前正在使用带有RKTBXMLSerialization的RestKit与基于xml的宁静Web服务进行通信。 对象映射一个呼叫的响应时,我遇到一个非常特殊的问题。 这个特殊元素的属性与节点本身的名称相同,例如:
<List ListName="List1">
<Item Name="ItemName1" Item="ItemValue1" />
<Item Name="ItemName2" Item="ItemValue2" />
</List>
我对应的映射如下所示:
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。 有没有人有这个问题,甚至解决方案?
正如我已经发布的那样,我现在也尝试了RKXMLReaderSerialization,问题消失了。 解决方案在代码方面正在改变
[RKMIMETypeSerialization registerClass:[RKTBXMLSerialization class] forMIMEType:@"application/xml"];
至
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
我将在GitHub上发布有关RKTBXMLSerialization的问题,因此也许在将来的版本中已解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.