watch this  

the official mrchucho blog

XPath and Cocoa

Posted 2005 Nov 09

I’m so glad Tiger included updates to Cocoa that added support for XQuery and XPath! It just makes using XML all that much easier.

While working on my Weather Plugin I realized that some stations include the latitude and longitude in their current observations XML. I thought it would be nice to help the user by loading these values into the Forecast preferences so they don’t have to hunt them down separately. So, instead of having to parse the whole document, I can just do the following (where xml is the NSXMLDocument I download):

    NSString *xpath = @".//latitude/text() | //longitude/text()";
    NSArray *nodes = [xml nodesForXPath:xpath error:nil];
    if([nodes count]&lt;1) {
    } else {
        [defaults setObject:[NSString stringWithFormat:@"%@",
            [nodes objectAtIndex:0]]
        [defaults setObject:[NSString stringWithFormat:@"%@",
            [nodes objectAtIndex:1]]

For more info on using XPath and XQuery with an NSXMLNode: Querying an XML Document

Responses to "XPath and Cocoa"

No responses yet.

Comments are now closed.
atom rss