Just a quick post here due to time constraints.
I don’t have time to submit this properly through GitHub because I haven’t cloned the repo, but I did want to share it since finding and using this class saved me a few hours of effort.
This is a small extension to SMXMLDocument (a very useful iOS XML parser, thanks, Nick) which will return all children for a given path (specified as an array of strings), not just the first match it finds.
- (NSArray *)descendantsWithPath:(NSArray *)path {
NSMutableArray *lineage = [NSMutableArray arrayWithArray:path];
NSMutableArray *array = [NSMutableArray array];
NSArray *kids = [self childrenNamed:[lineage objectAtIndex:0]];
[lineage removeObjectAtIndex:0];
if ([kids count] > 0) {
if (0 == [lineage count]) {
// bottom of path
[array addObjectsFromArray:kids];
} else {
// recurse into path
for (SMXMLElement *el in kids) {
NSArray *elements = [el descendantsWithPath:lineage];
if ([elements count] > 0)
[array addObjectsFromArray:elements];
}
}
}
return array;
}
This can be easily extended to:
- (NSArray *)descendantsWithPath:(NSArray *)path andAttribute:(NSString *)attribute
To find only leaf nodes on the given path with a specific attribute, but I haven’t gotten that far in my own project yet–possibly a future update to this post.
Note: This was based on the master-arc branch supporting ARC.
Enjoy!