0

处理完 XML 数据(读取 xml 数据并插入 sqlite 读取数据......等)后,我想转到下一个视图。

如何检查是否已完成 XML 解析?

我附上我的代码。我在哪里可以检查解析是否完成?

-(IBAction)clickServerSync:(id)sender
{
    if ([util checkNetwork]) {                  
        [self LoadXml:@"LANG"];
        [self LoadXml:@"CATEGORY"];
        [self LoadXml:@"LIST"];
        [self LoadXml:@"FILE"];        
    }  
}

-(void)LoadXml:(NSString*)P_VAL
{    
    [indicator setHidden:NO];     
    NSString *smsURL = [NSString stringWithFormat:@"%@%@.asp", XML_URL, P_VAL];   

    NSString *sendAuthInfo = [NSString stringWithFormat:@"xx=%d" , 0 ];    
    NSString *val = [sendAuthInfo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:smsURL]]autorelease];    
    [request setURL:[NSURL URLWithString:smsURL]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [val dataUsingEncoding:NSUTF8StringEncoding]];

    [self startAsyncLoad:request tag:P_VAL];   
} 
- (void)startAsyncLoad:(NSMutableURLRequest*)request tag:(NSString*)tag {    
    CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];    
    if (connection) {        
        [receivedData setObject:[[NSMutableData data] retain] forKey:connection.tag];        
    }    
}

- (NSMutableData*)dataForConnection:(CustomURLConnection*)connection {    
    NSMutableData *data = [receivedData objectForKey:connection.tag];    
    return data;
}

-(void)check_xmlParserEnd 
{  
    [indicator setHidden:YES];     
}


- (void)connection:(CustomURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [dataForConnection setLength:0];
}

- (void)connection:(CustomURLConnection *)connection didReceiveData:(NSData *)data {

    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [dataForConnection appendData:data];

}

-(void)connectionDidFinishLoading:(CustomURLConnection*)connection
{   
    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [connection release]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithData:dataForConnection] autorelease];

    XMLParser *parser = [[XMLParser alloc] initXMLParser];  
    [xmlParser setDelegate:(id)parser]; 
    parser.viewDelegate = (id)self;  
    [xmlParser parse ];  

}
4

2 回答 2

2

NSXMLParser是一个同步解析器。一旦[xmlParser parse]返回,解析就完成了。

于 2012-08-26T08:33:25.760 回答
0

由于您使用 NSXMLParser 并将委托设置为 self,因此您的类应该实现 NSXMLParserDelegate 方法,并且在所有完成后调用“--parserDidEndDocument:”。

于 2012-08-27T09:36:44.470 回答