Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:57:27+00:00 2026-05-11T18:57:27+00:00

I have an method that reads an xml file and stores the xml nodes

  • 0

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];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:57:27+00:00Added an answer on May 11, 2026 at 6:57 pm

    Here’s how I’d write it:

    for (NSXMLElement *categoryElement in nodes) {
        NSManagedObject *newObject = [catArrayController newObject];
        [newObject setValue:[categoryElement stringValue] forKey:@"name"];
        [catArrayController addObject:newObject];
        [newObject release];
    }
    

    First, I’m using the Objective-C 2.0 for-each syntax. This is simpler than using index variables. I eliminated i and arrayCount.

    Next, I took out your NSAutoreleasePool. None of the objects in the loop are autoreleased, so it had no effect. (The newObject method returns a retained object which is, by convention, what methods with the word new in their name do) This is also why I release newObject after 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 renamed newObject) as an NSString. Core Data objects are always either an instance of NSManagedObject or a subclass of NSManagedObject.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you considered not escaping the data until it hits… May 12, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer Use the OLS class [http://www.scipy.org/Cookbook/OLS] from the SciPy cookbook. May 12, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer Yes, ContentType has FieldRefs for all the fields (Well, not… May 12, 2026 at 6:06 am

Related Questions

I come across this problem when i am writing an event handler in SharePoint.
Trying to parse an HTML document and extract some elements (any links to text
What's the ideal way of generating XML without creating and saving a file? I'm
Basically, I have a class with 2 methods: one to serialize an object into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.