objective-c: Webservice, How to get two elements after parsing through NSXMLParser?
I am using the below code which gives me the string of one element after parsing, while I want to get another element's text on the base of first element's result.
e.g. I am getting bool value from ResponseType and now on the base of that value I want to get another element's ("TotalAmt") value. and if ResponseType is 0 anohter field appears in code which is DeclineReason and then I want to get DeclineReason's value.
<AuthorizeResult>
<ResultCode>Success</ResultCode>
<RequestTime>2011-09-30T01:42:54.1834586-04:00</RequestTime>
<ResultObj>
<RequestId>55258</RequestId>
<ResponseType>1</ResponseType>
<UserType>Permanent</UserType>
<RequestDate>2011-09-30T05:42:54.481</RequestDa开发者_JAVA百科te>
<TransactionId>0081315926057572</TransactionId>
<TransactionCode>001440</TransactionCode>
<TotalAmt>1</TotalAmt>
<UserId>47</UserId>
</ResultObj>
</AuthorizeResult>
I code this but its not workin g and get only the value of "ResponseType"
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if ([elementName isEqualToString:@"ResponseType"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
elementFound = YES;
}
}
}
//---when the text in an element is found---
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (elementFound)
{
[soapResults appendString: string];
}
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"ResponseType"])
{ //---displays the country---
NSLog(@"%@", soapResults);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Response Type!" message:soapResults delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[soapResults setString:@""];
elementFound = FALSE;
}
}
I call [xmlParser parse] two times.
qelementName = @"ResponseType";
[soapResults setString:@""];
[xmlParser parse];
ResponseType =[[NSString alloc] initWithFormat:@"%@", soapResults];
qelementName = @"TotalAmt";
[soapResults setString:@""];
[xmlParser parse];
TotalAmt = [[NSString alloc] initWithFormat:@"%@", soapResults];
and it works.
How about
if ([elementName isEqualToString:@"ResultObj"] || [elementName isEqualToString:@"Reason"])
in both didStartElement and didEndElement?
精彩评论