Thursday, October 22, 2009

XML Reader (4)



The is the fourth post in a series. The others are (here, here and here). We're reading XML today.

Actually, we've finished reading it and now we want to filter the elements to get just what we want. One thing of interest is the code for sorting all the keys in the dictionary (helpful for printing in debug phase). It seems like there should be an instance method for that in NSArray, but there isn't. The method is called sortedKeys.

Since we're going to be writing to a plist, I substitute the string @"None" when a value is nil. Of course, if I ever come across a record with that as an authentic value, I'll be screwed. The correct way to do this for collection objects is to use NSNull, then process it appropriately when displaying to the screen. But you still can't save it in a plist. Probably, I should use @"TESpecialValueThatSimulatesNullAndCanBeStoredInAPlist". You'd never run into that in any other context.

That's it. I need to test this a lot more, and then try to build an app that will take a PMID (say by reference to a PubMed record shown in a browser view), fetch the XML, process it, and then display it nicely in the GUI. If I were sadistic I would try to build a lightweight alternative to Reference Manager. (Just look at that website---it's so ugly only a mother could love it). Many years ago, they basically destroyed my system with one of their early efforts and I've never used it since. But, the thing is, for such an app to be useful you'd have to interface with Microsoft Word. I may be sadistic but I'm not crazy!




....

- (NSMutableArray *)sortedKeys {
NSMutableArray *mA = [[mD allKeys] mutableCopy];
SEL theSel = @selector(caseInsensitiveCompare:);
[mA sortUsingSelector:theSel];
return mA;
}

- (void)reportAll {
for (cKey in [self sortedKeys]) {
NSLog(@"%@", cKey);
NSLog(@"%@", [mD objectForKey:cKey]);
}
}

- (NSMutableDictionary *)processEntries{
NSMutableDictionary *mD2;
mD2 = [NSMutableDictionary
dictionaryWithCapacity:10];
NSArray *keyL;
keyL = [NSArray arrayWithObjects:
@"PMID",
@"MedlineTA",
@"Title",
@"Volume",
@"MedlinePgn",
@"Year",
@"Abstract",
@"AbstractText",
nil];
NSString *s;
NSString *k;
NSString *k2;
id obj;
for (k in keyL) {
obj = [mD objectForKey:k];
if ([obj isEqualToString:@"multiples"]) {
k2 = [k stringByAppendingString:
[NSString stringWithFormat:@"%i",1]];
obj = [mD objectForKey:k2];
}
if (!obj) { obj = @"None"; }
[mD2 setObject:obj forKey:k];
}
// finally, build the author list
int i;
int N = [[keyCountD
objectForKey:@"LastName"] intValue];
keyL = [NSArray arrayWithObjects:
// some have one, some the other!
@"FirstName",
@"ForeName",
@"Initials",
@"LastName",nil];

NSMutableArray *mA;
mA = [NSMutableArray arrayWithCapacity:5];
NSMutableDictionary *mD3;

for (i = 1; i < N + 1; i++) {
mD3 = [NSMutableDictionary
dictionaryWithCapacity:3];
for (k in keyL) {
k2 = [k stringByAppendingString:
[NSString stringWithFormat:@"%i",i]];
s = [mD objectForKey:k2];
if (!s) { s = @"None"; }
[mD3 setObject:s forKey:k];
}
[mA addObject:mD3];
}
[mD2 setObject:mA forKey:@"authorList"];
return mD2;
}

@end