How to insert xml parsed contents into Core Data
I am developing an application. I need some help I cannot insert my nsxml parsed contents into Core Data. Is there a specific way to do it?
My code is
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([@"forecast_conditions" isEqualToString:elementName]) {
isParsingForecast = NO;
NSManagedObjectContext *moc=[self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Weather" inMana开发者_StackOverflow中文版gedObjectContext:moc];
if([elementName isEqualToString:@"low"])
{
}
}
else if([@"forecast_information" isEqualToString:elementName]){
isParsingInformation=NO;
}
}
I am stuck and do not know how to get the insert done.
http://www.google.com/ig/api?weather=india thats my xml
I wish to insert the forecast_condition data into coredata into an entity Weather which I have attributes as high, low, etc.
Hard to provide specifics without knowing anything about your data model or what your trying to do but it would look something like this assuming your Weather
entity has an attribute called pressure
:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([@"forecast_conditions" isEqualToString:elementName]) {
isParsingForecast = NO;
NSManagedObjectContext *moc=[self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Weather" inManagedObjectContext:moc];
if([elementName isEqualToString:@"low"])
{
[entity setValue:elementName forKey:@"pressure"];
}
}
else if([@"forecast_information" isEqualToString:elementName]){
isParsingInformation=NO;
}
}
In short, you need to take values from the parsed xml and put them into your managed objects with the appropriate key.
Check out Jim Dovey's streaming XML parser. He also has an example app that shows how to use it with core data
https://github.com/AlanQuatermain/ParserExample
精彩评论