I have an method that reads an xml file and stores the xml nodes at a certain XPath-path in an NSArray called *nodes. What I want to do is take each one of the items in the array and add it to a core data entity called Category with the attribute of “name”.
I have tried a number of different ways of creating the entity but I’m not sure about the correct way to do this effectively. This is the code used to create the NSArray, any ideas on how to implement this? (ignore the NSError, I will fix this in the final version)
- (IBAction)readCategories:(id)sender
{
NSString *xmlString = [resultView string];
NSData *xmlData = [xmlString dataUsingEncoding: NSASCIIStringEncoding];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:xmlData options:nil error:nil];
//XPath
NSError *err=nil;
NSArray *nodes = [xmlDoc nodesForXPath:@"//member[name='description']/value/string" error:&err];
}
EDIT – My loop code
NSArray *nodes = [xmlDoc nodesForXPath:@"//member[name='description']/value/string" error:&err];
int arrayCount = [nodes count];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSXMLElement *categoryEl;
NSString *new = [catArrayController newObject];
int i;
for (i = 0; i < arrayCount; i++)
{
[categoryEl = [nodes objectAtIndex:i]];
[new setValue:[categoryEl stringValue] forKey:@"name"];
[catArrayController addObject:new];
}
[pool release];
Here’s how I’d write it:
First, I’m using the Objective-C 2.0 for-each syntax. This is simpler than using index variables. I eliminated
iandarrayCount.Next, I took out your
NSAutoreleasePool. None of the objects in the loop are autoreleased, so it had no effect. (ThenewObjectmethod returns a retained object which is, by convention, what methods with the wordnewin their name do) This is also why I releasenewObjectafter adding it to the array controller. Since I’m not going to be using it any more in this method, I need to release it.Also, you had defined
new(which I renamednewObject) as anNSString. Core Data objects are always either an instance ofNSManagedObjector a subclass ofNSManagedObject.Your line
[categoryEl = [nodes objectAtIndex:i]]won’t compile. That’s because the bracket syntax is used to send a message to an object. This is an assignment statement, so the bracket syntax is not needed here. (This line is also not necessary any more because of I’ve changed the loop to use the for-each syntax) But, for future reference,categoryEl = [nodes objectAtIndex:i];would have worked.