如何在iphone sdk中的某个时刻停止libxml解析器中的连接?

时间:2010-06-21 09:53:51

标签: objective-c

我需要在某个时刻停止libxml解析器的连接。任何人都可以建议我如何做到这一点。

这是我用来建立连接的方法。

 - (BOOL)parseWithLibXML2Parser 
{
BOOL success = NO;
ZohoAppDelegate *appDelegate = (ZohoAppDelegate*) [ [UIApplication sharedApplication] delegate];
NSString* curl;
if ([self.lateFeeName length] == 0) 
{
    curl = @"https://invoice.zoho.com/api/view/settings/latefees?ticket=";
    curl = [curl stringByAppendingString:appDelegate.ticket];
    curl = [curl stringByAppendingString:@"&apikey=bfc9c6dde7c889a19f8deea9d75345cd"];
    curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:curl]];

NSLog(@"the request parserWithLibXml2Parser %@",theRequest);
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

self.connection = con;
[con release];
// This creates a context for "push" parsing in which chunks of data that are 
// not "well balanced" can be passed to the context for streaming parsing. 
// The handler structure defined above will be used for all the parsing. The
// second argument, self, will be passed as user data to each of the SAX
// handlers. The last three arguments are left blank to avoid creating a tree
// in memory.
_xmlParserContext = xmlCreatePushParserCtxt(&simpleSAXHandlerStruct, self, NULL, 0, NULL);
if(self.connection != nil) 
{
    do 
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } while (!_done && !self.error);
}
if(self.error) 
{
    [self.delegate parser:self encounteredError:nil];
} else 
{
    success = YES;
}
return success;
}

实际上,当我正在处理数据加载时点击后退按钮时,我收到异常。

// Called when a chunk of data has been downloaded.
- (void)connection:(NSURLConnection *)connection 
didReceiveData:(NSData *)data 
{
// Process the downloaded chunk of data.
xmlParseChunk(_xmlParserContext, (const char *)[data bytes], [data length], 0);//....Getting Exception at this line.
}

任何人的帮助都会让我非常感激。

谢谢你, Monish。

1 个答案:

答案 0 :(得分:2)

libxml2 API中提供的xmlStopParser - 函数可能就是您要查找的内容。

在文档中指定为void xmlStopParser(xmlParserCtxtPtr context)

文档可在此处获得: http://xmlsoft.org/html/libxml-parser.html#xmlStopParser

另外,Alex Deem在这个帖子中有一个很好的提示: Is it possible to stop a libxml parser while the process is running?