RestKit XML Mapping with same element and attribute name

363 views Asked by At

I'm currently using RestKit with RKTBXMLSerialization to communicate with a restful xml-based web service. I have a very special problem with object mapping the response for one call. This special element has an attribute with the same name as the node itself, let's say:

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

My corresponding Mapping looks like this:

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]];

Using these model classes:

@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

The error message I get is:

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

I already tried pretty much and can track the problem down to the duplicate name 'Item' in the xml structure. As I can't influence the response schema I need a client side solution.

The problem could also be inside RKTBXMLSerialization (TBXML), so I will give RKXMLReaderSerialization a try. Did anybody already had this problem or even a solution?

1

There are 1 answers

0
1024kilobyte On BEST ANSWER

As I already posted I now also tried RKXMLReaderSerialization and the problem was gone. The solution code-wise was changing

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

to

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

I'll post the issue with RKTBXMLSerialization on GitHub, so maybe it's getting fixed in a future version.