2012-01-31 5 views
0

XML을 파싱하기위한 별도의 클래스가 있습니다. 서버에서 가져옵니다.모델 클래스의 NSXMLParser에서 결과가 없습니다.

#import "CheckLoginModel.h" 
#import "Common.h" 
#import "Utils.h" 
#import "Constants.h" 

@implementation CheckLoginModel 
@synthesize strUserID; 
@synthesize strUserName; 
@synthesize i; 
@synthesize dict; 

    -(void)CheckLogin:(NSString *)strDeviceToken 
{ 
    dict = [[NSMutableDictionary alloc]init]; 
     @try 
     { 
      NSString *soapMessage = [NSString stringWithFormat: 
            @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
            "<soapenv:Envelope \n" 
            "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n" 
            "xmlns:tem=\"http://tempuri.org/\"> \n" 
            "<soapenv:Header/>\n" 
            "<soapenv:Body>\n" 
            "<tem:CheckDeviceToken>\n" 
            "<tem:dt>%@</tem:dt>\n" 
            "</tem:CheckDeviceToken>\n" 
            "</soapenv:Body>\n" 
            "</soapenv:Envelope>\n",strDeviceToken]; 

      NSURL *url = [NSURL URLWithString:kMainURL]; 
      NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];    
      NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];   
      [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
      [theRequest addValue: @"http://tempuri.org/IService1/CheckDeviceToken" forHTTPHeaderField:@"Soapaction"]; 
      [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
      [theRequest setHTTPMethod:@"POST"];  
      [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
      NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
      if(theConnection) 
      { 
       webData = [[NSMutableData data] retain]; 
      } 
      else 
      { 
       NSLog(@"The Connection is NULL"); 
      } 
     }@catch (NSException *ex) { 
      [Utils LogExceptionOnServer:@"ChatApplicationAppDelegate" methodName:@"CheckLogin" exception:[ex description]]; 
     } 

} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"ERROR with theConenction"); 
    [connection release]; 
    [webData release]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",theXML); 
    [theXML release]; 

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities: YES]; 
    [xmlParser parse]; 
    [xmlParser release]; 

    [connection release]; 
    [webData release]; 

    //if(strUserName != NULL) 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"register" object:self userInfo:dict]; 
    //[dict release]; 

} 

#pragma mark - 
#pragma mark XML PARSING RELATED FUNCTIONS 
#pragma mark - 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName 
    attributes: (NSDictionary *)attributeDict{ 

    if([elementName isEqualToString:@"CheckDeviceTokenResult"]) 
    { 

    } 

    else if([elementName isEqualToString:@"a:UserID"]) 
    { 
     if(!soapResults) 
      soapResults = [[NSMutableString alloc] init]; 

    } 
    else if([elementName isEqualToString:@"a:UserName"]) 
    { 
     if(!soapResults) 
      soapResults = [[NSMutableString alloc] init]; 

    } 

} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 

    if([elementName isEqualToString:@"a:UserID"]) 
    { 
     i = [soapResults intValue]; 
     strUserID = soapResults; 
     soapResults = nil; 
     [dict setObject:strUserID forKey:@"id"]; 
    } 
    else if([elementName isEqualToString:@"a:UserName"]) 
    { 
     strUserName = soapResults; 
     soapResults = nil; 
     [dict setObject:strUserName forKey:@"name"]; 

    } 

} 


@end 

내 응용 프로그램을 디버깅 및 didEndElement에 도달, soapResult 나에게 아무것도주지 않는다 : 여기 내 모델 클래스입니다. 반대로 컨트롤러 클래스에서 동일한 코드를 사용하면 원하는 결과를 얻을 수 있습니다. 왜 그런지 궁금합니다.

답변

1

파서를 구현하지 않았습니다 : foundCharacters : 시작 요소에 문자열을 할당했지만 데이터를 가져 와서 foundCharacters에 soapResults를 설정해야합니다. soapResults는 어디에 할당합니까? 나는 그것을 할당하는 코드를 보지 못하기 때문에 이것이 무효입니다.

또한 didEndElement는 파서가 요소의 끝에 도달 할 때마다 발생합니다. 파싱이 끝났을 때가 아닙니다. 그것은 parserDidEndDocument입니다. 따라서 파서가 요소의 끝에 도달했지만 여전히 관심이있는 두 요소를 전달하지 않았을 가능성이 있습니다.

+0

고맙습니다. 어리석은 실수 였어. 그것을 가져와 주셔서 감사합니다. – Nitish

+0

괜찮습니다. 때로는 눈이 달라집니다. 눈의 신선한 세트로 코드를보기 위해 누군가를 호출 한 횟수를 셀 수 없다. :) – bryanmac