1

我正在尝试将RestKit 0.20-pre3RKXMLReaderSerializationXMLReader一起使用,以便从 WebService 映射 XML 响应,如下所示:

<ArrayOfAddressBookItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<AddressBookItem>
<CommonName>xxxxxxxxxx</CommonName>
<OU>xxxxxx</OU>
<Name>xxxxxx</Name>
<LastName>xxxxxxxxxx</LastName>
<Service>xxxxxxxxxx</Service>
<Email>xxxxxxxxxxxx</Email>
<InternalPhoneNumber>xxxxxxxxxxx</InternalPhoneNumber>
<ExternalPhoneNumber>xxxxxxxxxxx</ExternalPhoneNumber>
<Mobile>xxxxxxxxxxx</Mobile>
<Street>xxxxxxxxxxx</Street>
<PostalCode>xxxxxxxxxxx</PostalCode>
<City>xxxxxxx</City>
<County>xxxxxxxxx</County>
<SupervisorCommonName>
xxxxxxxxxxxxx
</SupervisorCommonName>
<SupervisorLastName>xxxxxxxxxx</SupervisorLastName>
</AddressBookItem>
<AddressBookItem>
<CommonName>yyyyyyyyyyyy</CommonName>
<OU>
yyyyyyyyyyyyy
</OU>
<Name>yyyyyyyyy</Name>
<LastName>yyyyyyyy</LastName>
<Service>yyyyyyyyyy</Service>
<Email>yyyyyyyyyy</Email>
<InternalPhoneNumber>yyyyyyyyy</InternalPhoneNumber>
<ExternalPhoneNumber>yyyyyyyy</ExternalPhoneNumber>
<Street>yyyyyyyyyyy</Street>
<PostalCode>yyyyyy</PostalCode>
<City>yyyyyy</City>
<County>yyyyyyyy</County>
<SupervisorCommonName>
yyyyyyyyyyy
</SupervisorCommonName>
<SupervisorLastName>yyyyyy</SupervisorLastName>
</AddressBookItem>
<AddressBookItem>
....
</AddressBookItem>
<AddressBookItem>
</ArrayOfAddressBookItem>

在应用委托代码中:

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://thehostaddress/mywebserviceurl/"]];
httpClient.parameterEncoding = AFFormURLParameterEncoding;
RKObjectManager *objManager = [[RKObjectManager alloc] initWithHTTPClient:httpClient];

[objManager setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];
objManager.requestSerializationMIMEType = RKMIMETypeTextXML;

RKObjectMapping *personMapping = [RKObjectMapping mappingForClass:[PersonItem class]];
[personMapping addAttributeMappingsFromDictionary:@{@"CommonName" : @"commonName", @"OU" : @"ou", @"Name" : @"name", @"LastName" : @"lastName", @"Service" : @"service", @"Email" : @"eMail", @"InternalPhoneNumber" : @"internalPhoneNumber", @"ExternalPhoneNumber" : @"externalPhoneNumber", @"Mobile" : @"mobilePhoneNumber", @"Street" : @"street", @"PostalCode" : @"postalCode", @"City" : @"city", @"County" : @"county", @"SupervisorCommonName" : @"supervisorCommonName", @"SupervisorLastName" : @"supervisorLastName"}];

RKResponseDescriptor *peopleResponse = [RKResponseDescriptor responseDescriptorWithMapping:personMapping pathPattern:@"/mywebserviceurl/" keyPath:@"ArrayOfAddressBookItem" statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objManager addResponseDescriptor:peopleResponse];

稍后,当我想获取对象时:

[objManager getObjectsAtPath:@"/mywebserviceurl/" parameters:nil
                     success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                     NSLog(@"SUCCESS: %@", mappingResult);
                     _items = [[mappingResult array] mutableCopy];
                 }
                 failure:^(RKObjectRequestOperation *operation, NSError *error) {
                     NSLog(@"ERROR: %@", error);
                 }];

我可以看到映射器获得了正确数量的 Array 元素,但是对于对象的每个字段,我无法检索值:

2012-12-10 19:02:53.370 GenPeople2[14240:1703] T restkit.object_mapping:RKMappingOperation.m:341 Mapping attribute value keyPath 'CommonName' to 'commonName' 
2012-12-10 19:02:53.370 GenPeople2[14240:1703] T restkit.object_mapping:RKMappingOperation.m:228 Found transformable value at keyPath 'CommonName'. Transforming from type '__NSDictionaryM' to 'NSString' 
2012-12-10 19:02:53.371 GenPeople2[14240:1703] T restkit.object_mapping:RKMappingOperation.m:360 Skipped mapping of attribute value from keyPath 'CommonName to keyPath 'commonName' -- value is unchanged ((null))

我的对象中的结果是空值。

我看到 XML 解析器给了我这个:

2012-12-10 19:02:53.371 GenPeople2[14240:1703] D restkit.object_mapping:RKMapperOperation.m:218 Asked to map source
 object {
     City =     {
         text = thecity;
     };
     CommonName =     {
         text = thename;
     };
     County =     {
         text = thecounty;
     }; 

等等....

如何正确映射值以允许 RestKit 检索 NSDictionary 中每个字段的值?

4

2 回答 2

2

感谢 Richard 的反馈,但它没有按我的意愿工作。真的很简单:在源对象中使用嵌套的键路径来映射,就像一个魅力:

RKObjectMapping *abItemMapping = [RKObjectMapping mappingForClass:[AddressBookItem class]];
[abItemMapping addAttributeMappingsFromDictionary:@{@"CommonName.text" : @"commonName", @"OU.text" : @"ou", @"Name.text" : @"name", @"LastName.text" : @"lastName", @"Service.text" : @"service", @"Email.text" : @"email"}];
于 2012-12-14T00:16:23.653 回答
0

我将元素的子节点映射为它们自己的对象。因此,例如 OU 将由它自己的映射和关系表示:

RKObjectMapping *baseValueMapping = [RKObjectMapping mappingForClass:[CHRValue class]];
[baseValueMapping addAttributeMappingsFromDictionary:@{@"text" : @"value"}];

RKRelationshipMapping *ouRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"OU" toKeyPath:@"ou" withMapping:baseValueMapping];

RKRelationshipMapping *nameRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Name" toKeyPath:@"name" withMapping:baseValueMapping];

[addressBookMapping addPropertyMapping:ouRelation];
[addressBookMapping addPropertyMapping:nameRelation];

其中 CHRValue 有一个名为“value”的属性,它是一个 NSString。请注意,您需要使用“文本”来引用节点的值。

于 2012-12-12T21:29:14.527 回答