简体   繁体   中英

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" inManagedObjectContext: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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM