Created
February 8, 2010 05:09
-
-
Save atr000/297890 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
void printUsage () { | |
printf("Usage: asxpath -file [XML file path] -xpath [XPath expression]\n"); | |
} | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
// getting the arguments passed on the command line | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
NSString *xmlFilePath = [userDefaults stringForKey:@"file"]; | |
if ((xmlFilePath == NULL) || ([xmlFilePath isEqualToString:@""] == YES)) { | |
printf("Error: XML file path is missing.\n"); | |
printUsage(); | |
return 1; | |
} | |
NSString *xpathExpr = [userDefaults stringForKey:@"xpath"]; | |
if ((xpathExpr == NULL) || ([xpathExpr isEqualToString:@""] == YES)) { | |
printf("Error: XPath expression is missing.\n"); | |
printUsage(); | |
return 1; | |
} | |
NSString *delimiter; | |
delimiter = [userDefaults stringForKey:@"delim"]; | |
if ((delimiter == NULL) || ([delimiter isEqualToString:@""] == YES)) { | |
delimiter = @"..."; | |
} | |
// does the passed file path exist? | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL isDir; | |
if ([fileManager fileExistsAtPath:xmlFilePath isDirectory:&isDir] == NO) { | |
printf("Error: The file does not exist: '%s'\n", [xmlFilePath UTF8String]); | |
return 1; | |
} else { | |
if (isDir == YES) { | |
printf("Error: The given path points to a directory: '%s'\n", [xmlFilePath UTF8String]); | |
return 1; | |
} | |
} | |
NSURL *fileURL = [NSURL fileURLWithPath:xmlFilePath]; | |
if (!fileURL) { | |
printf("Error: Cannot create an URL from file '%s'.", [xmlFilePath UTF8String]); | |
return 1; | |
} | |
// loading the XML file | |
NSError *err=nil; | |
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:fileURL options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&err]; | |
if (xmlDoc == nil) { | |
printf("Error: Could not create XML object from file '%s'.\n%s", [xmlFilePath UTF8String], [[err localizedDescription] UTF8String]); | |
return 1; | |
} | |
// executing the XPath expression | |
NSArray *matchNodes = [xmlDoc nodesForXPath:xpathExpr error:&err]; | |
if (matchNodes == NULL) { | |
if (err == nil) { | |
return 0; | |
} else { | |
printf("Error: %s.\n", [[err localizedDescription] UTF8String]); | |
return 1; | |
} | |
} | |
// no match | |
if ([matchNodes count] == 0) { | |
return 0; | |
} | |
// printing the result | |
id object; | |
int countMatchNodes = [matchNodes count]; | |
int i; | |
for (i = 0; i < countMatchNodes; i++) { | |
object = [matchNodes objectAtIndex:i]; | |
NSString *xml = [object XMLString]; | |
NSString *stripXML = [xml stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
if (i == countMatchNodes - 1) { | |
printf("%s\n", [stripXML UTF8String]); | |
} else { | |
printf("%s\n%s\n", [stripXML UTF8String], [delimiter UTF8String]); | |
} | |
} | |
[pool release]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment