While going through parsing a xml doc i have used multiple if-else to parse the data tag wise. I can do the same with for loop also. But i am confused about which to use so as the performance will increase. Which will be the faster one ?
//Parsing with If-Else
if(element.name isEqualToString:@"String1") {
object.id = [element.value intValue];
}
if(element.name isEqualToString:@"String2") {
object.name = element.value;
}
if(element.name isEqualToString:@"String3") {
object.address = element.value;
}
.....
// OR Parsing with for loop
for(NSString * str in NSArray) {
[dict setObject:element.value forKey:element.name];// This will add parsing element in dict
}
//Use the dictionary to show data later
This is really as much a question about representation as it is about code structure. An object with named properties strongly suggests an if-elseif structure, and a dictionary strongly suggests a loop.
XML (and all of it’s kin) are perfectly paired with name-value representation. Tags are names and tag bodies are values.
The dictionary is the way to go, and therefore so is the loop. This is the choice made by the NS parser as well as any third party I can think of.