A few lines on how to use the NSXMLDocument

        Because I needed to retrieve information from a Final Cut Pro project file saved in XML format, I wrote some lines of code. But before doing so, to figure out what XQuery statement to use, I implemented a helper program to load a XML document and test some XQuery statement. Apple’s XMLBrowser sample was helpful, but it doesn’t show an input XML file and the result of XQuery side by side. 

        So, I wrote one, and here I would like to the quick and dirty approach on how to use the NSXMLDocument.

        The NSXMLDocument is a tree parser, while the Apple also provides an event parser. The tree parser provides very helpful method to manipulate an XML file. One of the good thing is XQuery. So, in this short codes, it sends query to the NSXMLDocumet.

- (IBAction)openXMLFile:(id)sender
{
    int result;
    NSArray *filetypes = [NSArray arrayWithObject:@"xml"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
    
    [oPanel setAllowsMultipleSelection:NO];
    
    result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:filetypes];
    if ( result == NSOKButton) {
        NSString *aFile = [oPanel filename];
        [self createXMLDocumentFromFile:aFile];
        [sourceTextView setString:[NSString stringWithFormat:@"%@", xmlDoc]];
    }
    
}

- (void)createXMLDocumentFromFile:(NSString *)file
{
    NSError *err = nil;

    NSURL *furl = [NSURL fileURLWithPath:file];
    if( !furl )
    {
        NSLog(@"Can't create an URL from file %@.", file );
        return;
    }
    
    if( xmlDoc )
        [xmlDoc release];
    
    xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&err];
    if( xmlDoc == nil )
    {
        // in previous attempt, it failed creating XMLDocument because it 
        // was malformed.
        xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:NSXMLDocumentTidyXML error:&err];
    }
    if( xmlDoc == nil)
    {
        
        NSLog( @"Error occurred while creating an XML document.");
//        if(err)
//            [self handleError:err];
    }
    else
        NSLog(@"%@", xmlDoc);
}

And… here is the code to send XQuery statement to the NSXMLDocument

- (IBAction)executeQuery:(NSButton *)sender {
    
    [queryResultView setString:@""];
   
    if ([queryTextView string] != nil) {
        NSError *error;
        NSArray *result = [xmlDoc objectsForXQuery:
             [queryTextView string] constants:nil error:&error];
        if (result) {
            unsigned count = [result count];
            unsigned i;
            NSMutableString *stringResult = [[NSMutableString alloc] init];
            for (i = 0; i < count; i++) {
                [stringResult appendString:
                    [NSString stringWithFormat:@"%d: {\r", i]];
                [stringResult appendString:[[result objectAtIndex:i]
                    description]];
                [stringResult appendString:@"\r}\r"];
            }
            [queryResultView setString:stringResult];
            
            NSLog(@"%@", stringResult);

            [stringResult release];
        } else if (error) {
            NSLog(@"Error Occurred.");
            
            //[self handleError:error];
        }
        
    }
    
}
About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: