• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Parsing XML to CoreData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Parsing XML to CoreData


  • Subject: Parsing XML to CoreData
  • From: Siegfried <email@hidden>
  • Date: Thu, 24 Mar 2011 13:06:26 -0300

So, after discussing some methods to import and export a CoreData database, I ended up sticking with my initial XML idea.

Now I'm stuck in a problem when parsing that is probably simple, but I can't solve. Basically, my XML is like this:

<elem1>
    <child1>Value</child1>
    <child2>Value</child2>
    <child3>Value</child3>
</elem1>
<elem2>
    <child1>Value</child1>
    <child2>Value</child2>
    <child3>Value</child3>
</elem2>

When parsing with NSXMLParser, everything runs properly (well, almost) except for the fact that every property (child elements here) from every entity (elem here) gets the value from the last imported property, child3 in this case.

For me, that means my currentEntity ivar is keeping track of every entity created, in some way that I probably just managed to do due to little CoreData knowledge.

Here is the main code:

-(void)parser: ... didStartElement: {

    ...

    // Entity
    if ([elementName isEqualToString:kEntityContent]) {
        if (currentEntity) [currentEntity release]; // currentEntity is an ivar
        currentEntity = [[NSEntityDescription insertNewObjectForEntityForName:kContent
                            inManagedObjectContext:managedObjectContext] retain];
        return;
    }

    // Attribute
    if ([elementName isEqualToString:kAttrName]) {
        [currentXMLValue setString:@""]; // currentXMLValue is another ivar
        return;
    }

    ...
}

-(void)parser: foundCharacters:(NSString *)string {

    [currentXMLValue appendString:string];
}

-(void)parser: ... didEndElement {

    // Database - Save the Import
    if ([elementName isEqualToString:@"database"]) {

        NSError *error;
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"There was an error while saving "
                   "the imported database: %@", error);
        }
    }

    // Attribute
    if ([elementName isEqualToString:kAttrName]) {
        [currentEntity setValue:currentXMLValue forKey:kAttrName];
        return;
    }

    ...
}

Thanks in advance for any help.

Regards

Siegfried_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Parsing XML to CoreData
      • From: "Hank Heijink (Mailinglists)" <email@hidden>
  • Prev by Date: Re: Exporting and Importing CoreData
  • Next by Date: Re: Parsing XML to CoreData
  • Previous by thread: Finding per-process network usage?
  • Next by thread: Re: Parsing XML to CoreData
  • Index(es):
    • Date
    • Thread