【问题标题】:Try to recover from an error with TouchXML尝试使用 TouchXML 从错误中恢复
【发布时间】:2009-04-14 12:33:34
【问题描述】:

我正在尝试使用 TouchXML 进行 XPath 查询,但如果查询失败,程序会立即崩溃。

有什么办法可以防止这种情况并继续执行程序。

我知道 TouchXML 是基于 libxml2 并且该库有一个名为 XML_PARSE_RECOVER 的选项,但我不知道如何将它与 TouchXML 一起使用。

源代码

- (id)initWithData:(NSData *)d
{
    if (![super init])
        return nil;
    NSError *error;

    translation = [[WRTranslation alloc] init];
    parser = [[CXMLDocument alloc] initWithData:d options:0 error:&error];

    if (error)
      NSLog(@"initWithData error = true");

    return self;
}

- (void)dealloc
{
    NSLog(@"dealloc de WRTouchParser = %@", self);
    [translation release];
    [parser release];
    [super dealloc];
}

- (BOOL)queryData:(NSString *)s
{
    // Nécessaire pour faire une requête. Exemple : //x:div[@class="se"]
    NSDictionary *mappings = [NSDictionary dictionaryWithObject:@"http://www.w3.org/1999/xhtml" forKey:@"x"];
    NSArray *res = [parser nodesForXPath:s namespaceMappings:mappings error:nil];

    queryResult = [res description];

    return YES; 
}

我尝试像这样进行错误的 XPath 查询://x:div[@cla

我得到:

XPath error : Invalid predicate

【问题讨论】:

  • 你能发布你正在使用的代码和崩溃的细节吗?我没有使用过 TouchXML,但如果它崩溃了,听起来好像有问题。
  • 只有当我执行了错误的 XPath 查询时:比如 //x:div[@cla

标签: iphone libxml2 touchxml


【解决方案1】:

我用的是普通的

@try {
    ...
} @catch (NSException *e) {
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多