检查某些东西是否为零的更好方法?

时间:2011-03-10 15:43:37

标签: iphone objective-c cocoa-touch

我正在尝试使用TBXML解析XML文件。但是,此解析器没有内置错误检查,因此如果元素不存在则崩溃。这是我解析我的一个XML文件的方式:

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;
}

正如您所看到的,它正在为每个项目拨打两次电话。这对我来说似乎是一个巨大的浪费。我可以用任何方式对每件物品进行相同的验证而不做我现在正在做的事情吗?

2 个答案:

答案 0 :(得分:1)

这是一个较短的版本:

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;
}

使用枚举器更新版本:

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;
}

我不确定枚举器版本是否有效,但我不知道它应该解决您的问题。

答案 1 :(得分:1)

您可以执行以下操作:

TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];
if (Blah) {
    ...
}