On 14 Sep 2008, at 02:51, Nathan Kinsinger wrote:

On Sep 13, 2008, at 12:50 PM, Amy Heavey wrote:

I've got an NSXMLDocument *custdoc

but I can't work out how to actually access the data in the document, I've looked at NSXMLElement, NSXMLParser, NSXMLNode but I just can't work out the correct code.

sample xml:

<sesbuddycustimport>
−
<customer>
<first_name>Willow</first_name>
<last_name>Tree Crafts</last_name>
<address_1>99 Leggatts Way</address_1>
<city>Herts</city>
<zip>WD24 5NQ</zip>
<country>GBR</country>
<user_email>[EMAIL PROTECTED]</user_email>
</customer>
−

How can I set an NSString *firstName to the value?

I appreciate any help,

Many Thanks

Amy Heavey



If you are working with Hillegass's example he is putting the XPath path in the identifier of the table column and uses it in the tableView:objectValueForTableColumn:row: data source method. In your example above the path would be "sesbuddycustimport/customer/ first_name"


If you are working with the NSXMLDocument programatically then you need to call the nodesForXPath:error: method yourself. I would separate the individual customer items into an array first then you can get any info about any customer. Also keep in mind that NSXMLDocument is a subclass of NSXMLNode so you can call NSXMLNode methods on it.
        (warning, typed into mail)

NSArray *customerArray = [custdoc nodesForXPath:@".//customer" error:nil];
        if ([customerArray count) {
                for (NSXMLNode *customerNode in customerArray) {
NSArray *firstNameArray = [[customerNode nodesForXPath:@".// first_name" error:nil];
                        if ([firstNameArray count]) {
NSString *firstNameString = [firstNameArray objectAtIndex:0] stringValue];
                                // do something with the first name string
                        }
                        // get other strings
                        ...
                        // do something with the other strings
                        ...
                }
        }


There are other methods of getting info out of an NSXMLDocument (or XML data in general), but this is how I usually approach it. You didn't mention what you wanted to do with the string.

Hope this helps,
--Nathan

Overall I want to extract the data from the XML file, and create new instances of CoreData Entities with it.

I seem to keep getting errors along the lines of NSArray / NSString / NSXMLNode may not respond to -nodesForXpath / stringForPath:ofNode:

It's already in a loop, and
printf( "Customer: %s\n", [[obj description] cString] );

correctly print out the correct line,
Customer: <customer><first_name>Willow</first_name><last_name>Tree Crafts</last_name><address_1>99 Leggatts Way</address_1><city>Herts</ city><zip>WD24 5NQ</zip><country>GBR</ country><user_email>[EMAIL PROTECTED]</user_email></customer>

I just don't seem able to get the individual data,

Many Thanks
Amy_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to