user2586519
user2586519

Reputation: 250

How to parse SOAP xml response in iPhone programming

I am unable to parse soap response,I have tried many ways but not solved my problem,I need the value of USERNAME and PASSWORD. I am getting following response after soap request :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<AuthenticateResponse xmlns="http://tempuri.org/">
<AuthenticateResult>
<DocumentElement>   
<Status>
<USERNAME>True</USERNAME>
<PASSWORD>true</PASSWORD>
</Status>
</DocumentElement>
</AuthenticateResult>
</AuthenticateResponse>
</soap:Body>
</soap:Envelope>

Here is my code :

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

 if ( [elementName isEqualToString:@"AuthenticateResponse"]||[elementName    
     isEqualToString:@"AuthenticateResult"] || [elementName   isEqualToString:@"DocumentElement"])
 {
 statusArray = [[NSMutableArray alloc] init];
 }
else if ([elementName isEqualToString:@"Status"])
{
    authenticate = [[Authenticate alloc] init];
}
else if ([elementName isEqualToString:@"USERNAME"])
{
    currentElementValue = [[NSMutableString alloc] init];
}
else if ([elementName isEqualToString:@"PASSWORD"])
{
    currentElementValue = [[NSMutableString alloc] init];
}

}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentElementValue) 
{
    [currentElementValue appendString:string];
}

}

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

if( [elementName isEqualToString:@"USERNAME"])
{
    authenticate.userName = currentElementValue;
}
else if ([elementName isEqualToString:@"PASSWORD"])
{
    authenticate.strAuthenticate = currentElementValue;
}
 else if ([elementName isEqualToString:@"Status"]) {
     [statusArray addObject:authenticate];
}


}

How can i get the values of username and password,can someone help me to parse this. Thanks in advance.

Upvotes: 0

Views: 3296

Answers (2)

Mohammad Sadiq Shaikh
Mohammad Sadiq Shaikh

Reputation: 3200

Please see the demo prepared for iOS/OSX using sample web service.

It uses swift - SOAP request creator & response parser.

[DEMO][1]

Done using Alamofire & SWXMLHash library

Thanks

Upvotes: 0

Leijonien
Leijonien

Reputation: 1413

Try this:

Add 2 properties to your class.

@property (nonatomic, strong) NSString *elementValue;
@property (nonatomic, strong) NSMutableArray *authenticationValues;

Replace your current implementation by:

- (void)parseXML:(NSData *)xmlData {
    self.authenticationValues = [NSMutableArray array];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;
    [parser parse];
    NSLog(@"Authentication values: %@", self.authenticationValues);
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // Keep track of the current element value
    self.elementValue = string;
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    // If the element was 'username' or 'password', add the value to the authenticationValues array
    if ([elementName isEqualToString:@"USERNAME"] ||
        [elementName isEqualToString:@"PASSWORD"]) {
        [self.authenticationValues addObject:self.elementValue];
    }
}

To parse the file perform the method parseXML: When running this you should see the following output on the console:

Authentication values: (
    True,
    true
)

which are the values for USERNAME and PASSWORD.

Upvotes: 2

Related Questions