Re: Parsing XML to CoreData
Re: Parsing XML to CoreData
- Subject: Re: Parsing XML to CoreData
- From: "Hank Heijink (Mailinglists)" <email@hidden>
- Date: Thu, 24 Mar 2011 12:13:43 -0400
On Mar 24, 2011, at 12:06 PM, Siegfried wrote:
> 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>
[snip]
> 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.
> -(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;
> }
You need to copy currentXMLValue, because in your code, every item that refers to it ends up referring to the same mutable string, which will change every time the parser:foundCharacters: method is called.
NSString *newValue = [currentXMLValue copy];
[currentEntity setValue:newValue forKey:kAttrName];
[newValue release];
Assigning strings like you did works with immutable ones, but not with mutable ones.
Hank
_______________________________________________
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