Reputation: 21
I have been trying to map to xml for weather. The xml looks like this
<current_observation>
<observation_epoch>1433740800</observation_epoch>
<weather>Clear</weather>
<temp_c>24</temp_c>
<relative_humidity>61%</relative_humidity>
<wind_dir>North</wind_dir>
<wind_mph>0</wind_mph>
<visibility_km>N/A</visibility_km>
</current_observation>
My weather.h
@interface Weather : NSObject
@property (nonatomic , copy) NSString* weather;
@property (nonatomic , copy) NSString* temp_c;
@property (nonatomic , copy) NSString* relative_humidity;
@property (nonatomic , copy) NSString* wind_dir;
@property (nonatomic , copy) NSString* wind_mph;
@property (nonatomic , copy) NSString* visibility_km;
@property (nonatomic , copy) NSString* observation_epoch;
@end
My mapping function
- (RKObjectManager*) makeWeatherXMLMappingwithURL:(NSString*)mLinkURL{
//Map the Weather class
RKObjectMapping* weatherMapping = [RKObjectMapping mappingForClass:[Weather class]];
[weatherMapping addAttributeMappingsFromDictionary:@{
@"weather.text":@"weather",
@"temp_c.text":@"temp_c",
@"relative_humidity.text":@"relative_humidity",
@"wind_dir.text":@"wind_dir",
@"wind_mph.text":@"wind_mph",
@"visibility_km.text":@"visibility_km",
@"observation_epoch.text":@"observation_epoch"
}];
//register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping: weatherMapping
method: RKRequestMethodAny
pathPattern: nil
keyPath: @"current_observation"
//keyPath:@"rss.channel.item"
statusCodes: RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
//Initialize RestKit for xml (rss/feed) parsing & mapping
NSURL *baseURL = [NSURL URLWithString: mLinkURL];
//make a new instance of RKObjectManager (Parser which inherits from RKObjectManager)
RKObjectManager *objectManager = [Parser managerWithBaseURL:baseURL];
[objectManager setRequestSerializationMIMEType:RKMIMETypeTextXML];
// [objectManager setAcceptHeaderWithMIMEType:@"application/rss+xml"];
[objectManager setAcceptHeaderWithMIMEType:@"text/xml"];
// [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/rss+xml"];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/xml"];
//add the responseDescriptor to RKObjectManager
[objectManager addResponseDescriptor:responseDescriptor];
return objectManager;
}
My call
- (void) parseWeatherXMLwithURL:(NSString*)mLinkURL{
//Make the XML Mapping
RKObjectManager* objectManager = [self makeWeatherXMLMappingwithURL:mLinkURL];
//asychronous mapping (Calling getObjectsAtPath doesn't block the thread until it has completed)
[objectManager getObjectsAtPath:@""
parameters:nil
//Asynchronous Success block
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray *weather = mappingResult.array;
if (weather!=nil && [weather count]>0){
//Delegate response to processAsynchronousLiveStreamingRSSComplete Handler
[self.delegate performSelector:@selector(processAsynchronousXMLWeatherComplete:) withObject:(Item*)[weather objectAtIndex:0]];
}
else{
NSMutableDictionary* details = [NSMutableDictionary dictionary];
[details setValue:@"Data is not available!" forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:@"Data is not available!" code:200 userInfo:details];
//Delegate response to processAsynchronousLiveStreamingRSSFailed Handler
[self.delegate performSelector:@selector(processAsynchronousXMLWeatherFailed:) withObject:error];
}
}
//Asynchronous Failed block
failure:^(RKObjectRequestOperation *operation, NSError *error) {
//Delegate response to processAsynchronousLiveStreamingRSSFailed Handler
[self.delegate performSelector:@selector(processAsynchronousXMLWeatherFailed:) withObject:error];
}];
}
After my call i get 1key/value pair which is @"current_observation":@"0 objects". I think that this means it maps to the root element - current-observation - but it cannot map to the rest of the elements.
Upvotes: 1
Views: 239
Reputation: 21
Finally i got the solution. I had to rename my class "weather" to "WeatherCO" -can be renamed to anything-, clean and rebuild my project and everything was fixed!
Upvotes: 1