Kirdok
Kirdok

Reputation: 1914

XML parsing problems in iOS

I am pulling information from web service and I am parsing the XML string, but in my code the XML part is calling 4 times,i don't know why.

Here is my code;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSLog(@"Data has been loaded");

NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];


NSLog(@"Respose Data :%@",responseString) ;


NSData *xmlData = [responseString dataUsingEncoding:NSASCIIStringEncoding];

xmlParser = [[NSXMLParser alloc] initWithData:xmlData];

[xmlParser setDelegate:self];

[xmlParser parse];

}

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

// @"Element started %@",elementName);
currentElement=elementName;  
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

count++;

if ([temp isEqualToString:@"true"]) {

    [SVProgressHUD showSuccessWithStatus:@"Saved"];

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(loginPage) userInfo:nil repeats:NO];

}else{
    [SVProgressHUD dismiss];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Don't Save"
                                                       delegate:self
                                              cancelButtonTitle:@"Okay"
                                              otherButtonTitles:nil, nil];

    [alertView show];

}

NSLog(@"count = %i",count);
currentElement=@"";
temp = @"";
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

temp = [temp stringByAppendingString:string];

}

And here is my XML return string;

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><signUpResponse xmlns="http://tempuri.org/"><signUpResult>true</signUpResult></signUpResponse></s:Body></s:Envelope>

Count output is ; count = 1 count = 2 count = 3 count = 4

What am i doing wrong? Thank you for your answer and interest.

Upvotes: 0

Views: 123

Answers (1)

chancyWu
chancyWu

Reputation: 14383

There are 4 end tags in your xml, namely </signUpResult></signUpResponse></s:Body></s:Envelope>, and the didEndElement method will be called when reading every end tag, so the count is 4.

In your case, you can include your code in a if statement to avoid being called 4 times. the code is as following:

if([elementName isEqualToString:@"signUpResult"]){
    if ([temp isEqualToString:@"true"]) {

        [SVProgressHUD showSuccessWithStatus:@"Saved"];

        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(loginPage) userInfo:nil repeats:NO];

    }else{
        [SVProgressHUD dismiss];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Don't Save"
                                                       delegate:self
                                              cancelButtonTitle:@"Okay"
                                              otherButtonTitles:nil, nil];

        [alertView show];

    }
}

Upvotes: 2

Related Questions