I’m trying to parse a SOAP response using RestKit. I’m able to successfully convert the SOAP XML response into objects if I define the mappings and relationships myself. But, I was wondering if it’s possible to use introspection (or something else) to automatically convert the SOAP XML response into an Objective-C object.
Sample XML:
<return>
<userId xsi:type="xsd:int">1113050187</userId>
<location xsi:type="ns1:Location">
<city xsi:type="xsd:string">San Francisco</city>
<state xsi:type="xsd:string">California</state>
</location>
</return>
I would like to convert this XML into this object:
@interface Return : NSObject
@property (strong, nonatomic) NSInteger userId; // attribute
@property (strong, nonatomic) Location *location; // relation
@end
The closest thing I could come up with is using introspection on the Return class to get all properties and using something like this for each attribute:
[mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:@”userId.text” toKeyPath:@”userId”]];
And for relations, I could again use introspection to find out all my class objects and use mapRelationship:withMapping: on each one
I ended up defining a method that recursively maps the properties to XML nodes if their names match. The naming convention and the data type of the property is important for this to work.
I’ve tried my best to clean this up before I post it here, but let me know if you need any help.
And then to map it automatically call it as: