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>
−
<customer>
<first_name>L****</first_name>
<last_name>Lin****</last_name>
<address_1>9****</address_1>
<city/>
<zip>S***</zip>
<country>GBR</country>
<user_email>la**********</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
                        ...
                }
        }

The ".//" will find all xml elements with that name at any depth in the node.


I do this often enough that I created a category on NSXMLNode to help get the string for an element:

- (NSString *)bb_StringFromXPath:(NSString *)nodePath
{
        NSArray *nodeArray = [self nodesForXPath:nodePath error:nil];
return [nodeArray count] ? [[nodeArray objectAtIndex:0] stringValue] : nil;
}
(Note that this will only return the string of the first xml element that the XPath returns, so make sure to use it on nodes that you know only have one such element)

So in the example above, inside the for loop I would do this:

NSString *firstNameString = [customerNode bb_StringFromXPath:@".// first_name"];


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


_______________________________________________

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