I am learning RestKit. I came across the elementToPropertyMapping feature which it provides. I am wondering whether I can use this mapping for xml documents? or is it just for JSON?
In the tutorial I found they are providing a JSON feed. So I am confused. Also Is there any other XML parser for IOS which provides this mapping feature. Thanks
The tutorial that you found is out of date. elementToPropertyMappings is no longer used. Instead of inheriting from RKObject or RKManagedObject, you use the normal objective-c classes and map data to them with an RKObjectMapping.
You can find the new documentation here.
But yes, you can use RestKit's object mapping for XML documents. The parser should detect the mimetype and use an xml parser instead of json; if it doesn't you can explicitly tell it to use xml as per the linked documentation.
In fact, the tutorial at gitHub is out of date, but even the new one doesn't have a reference of how to effectively query XML files with RestKit to do a correct mapping and this is very confusing for the newcomers like me.
As a quick reference, I will try to show you the way it works with XML:
Say you have a response like this:
<?xml version="1.0" encoding="UTF-8"?>
<users type="array">
<user>
<id>1</id>
<name>Roberto</name>
<company>Apple</company>
</user>
<user>
<id>2</id>
<name>Carlos</name>
<company>Adobe</company>
</user>
</users>
If you want to map the a class Users in your Objective-C Project, you will have to pay special attention to the keyPath, in this case: "users" <- first tag, and "user" <-second tag, and the "Class" you want to map.
For this, you use:
[[RKObjectManager sharedManager].mappingProvider setMapping:objectMapping forKeyPath:@"users.user"];
For the rest, is ok if you use the first part of the tutorial on this link (Object Mapping by Example):
https://github.com/RestKit/RestKit/wiki/Object-mapping
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With