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:
Updated version using enumerator:
I’m not sure if enumerator version works, but off top of my head it should solve your problem.