Better way to check if something is nil?
I'm trying 开发者_开发知识库to parse a XML file using TBXML
. However, this parser has no error-checking built in so if an element doesn't exist it crashes. Here's how I'm parsing one of my XML files:
TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
if (XML.rootXMLElement) {
TBXMLElement *XMLRoot = XML.rootXMLElement;
if ([TBXML childElementNamed:@"blah" parentElement:XMLRoot]) {
TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];
if ([TBXML childElementNamed:@"stuff" parentElement:Blah]) {
TBXMLElement *Item = [TBXML childElementNamed:@"item" parentElement:Blah;
if ([TBXML childElementNamed:@"stuff:blah" parentElement:Item]) {
TBXMLElement *something = [TBXML childElementNamed:@"stuff:blah" parentElement:Item];
NSString *Something = [TBXML textForElement:something];
//do something here...
}
else {
[self showFetchError];
[XML release];
return;}
} else {
[self showFetchError];
[XML release];
return;}
} else {
[self showFetchError];
[XML release];
return;}
} else {
[self showFetchError];
[XML release];
return;
}
As you can see, it's making a call twice for each item. That seems like a huge waste of overhead to me. Any way I can do the same verification of each item without doing what I'm doing right now?
Here's a shorter version:
BOOL success = NO;
TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
if (XML.rootXMLElement) {
TBXMLElement *XMLRoot = XML.rootXMLElement;
if ([TBXML childElementNamed:@"blah" parentElement:XMLRoot]) {
TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];
if ([TBXML childElementNamed:@"stuff" parentElement:Blah]) {
TBXMLElement *Item = [TBXML childElementNamed:@"item" parentElement:Blah;
if ([TBXML childElementNamed:@"stuff:blah" parentElement:Item]) {
success = YES;
TBXMLElement *something = [TBXML childElementNamed:@"stuff:blah" parentElement:Item];
NSString *Something = [TBXML textForElement:something];
//do something here...
}
}
}
}
if (!success) {
[self showFetchError];
[XML release];
return;
}
Updated version using enumerator:
TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
NSArray *path = [NSArray arrayWithObjects:@"blah", @"item", @"stuff:blah", nil];
NSEnumerator *e = [path objectEnumerator];
TBXMLElement *currentNode = XML.rootXMLElement;
BOOL success = NO;
while ((NSString *node = [e nextObject]) && currentNode) {
if ([node isEqualToString:[path lastObject]]) {
success = YES;
NSString *Something = [TBXML textForElement:currentNode];
// do the last element thing with string
} else {
currentNode = [TBXML childElementNamed:node parentElement:currentNode];
}
}
if (!success) {
[self showFetchError];
[XML release];
return;
}
I'm not sure if enumerator version works, but off top of my head it should solve your problem.
You can do things like:
TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];
if (Blah) {
...
}
精彩评论