Mujtaba Mujtaba - 4 months ago 15
iOS Question

How to get JSON Data from XML tag in objective c

I am trying to do validation from server
code is

-(IBAction)LoginbtnClick:(id)sender
{
NSString* username = usertxt.text;
NSString* pass = passtxt.text;


if([usertxt.text isEqualToString:usertxtstr ]&& [passtxt.text isEqualToString:passtxtstr])
{
MenuScreen *MenuS =[[MenuScreen alloc] initWithNibName:@"MenuScreen" bundle:nil];
[self.navigationController pushViewController:MenuS animated:YES];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:appDelegate.tab];
}

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<Login xmlns=\"http://tempuri.org/\">"
"<UserCode>%@</UserCode>"
"<Password>%@</Password>"
"</Login>"
"</soap:Body>"
"</soap:Envelope>",username,pass];

NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];


NSURL *url = [NSURL URLWithString:@"http://URL?op=Login"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
webData = [[NSMutableData data] init];

}
else
{

}
[usertxt resignFirstResponder];
[passtxt resignFirstResponder];
}
#pragma mark - NSURLConnection Delegate

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

[webData setLength:0];


}

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

[webData appendData:data];


}

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

NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"loginStatus =%@",loginStatus);
greeting.text = loginStatus;

}

#pragma mark - NSXMLParsing Delegate

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"UserCode"])
{
myDataClassObj=[[mydata alloc]init];
}
}

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

myMutableStringSiteObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringSiteObj);
NSData *datasite = [myMutableStringSiteObj dataUsingEncoding:NSUTF8StringEncoding];
responseSitedict = [NSJSONSerialization JSONObjectWithData:datasite options:0 error:nil];
NSLog(@"JSON DATA = %@",responseSitedict);

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"DataArray: %@",myDataNSMArray);
}


I am getting value like

loginStatus =<?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><LoginResponse xmlns="http://tempuri.org/"><LoginResult>[{"UserId":1,"UserCode":"abc","Password":[31,0,226,51,21,211,31,31,231,41,121,221,171,201,11,201,21,201,181,51],"SaltValue":[],"RoleId":0,"Salutation":"","FirstName":"abc","LastName":"xyz","EmailId":"abc@gmail.com","MobilNo":"9876543210","Status":true}]</LoginResult></LoginResponse></soap:Body></soap:Envelope>


I want that UserCode and Password to compare with my username and password and if it is true user get login.
Not getting how to take it from xml tag. Please Help.

Answer

You can pass your dictionary like this

First Convert it into dictionary

 NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatus error:&parseError];
NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
recordResult = [xmlDictionary[@"success"] integerValue];
NSLog(@"Success: %ld",(long)recordResult);

then do like this

NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
    NSLog(@"Address Dict = %@",Address);
    NSDictionary *new =[Address objectForKey:@"soap:Body"];
    NSLog(@"NEW DICT =%@",new);
    NSDictionary *LoginResponse=[new objectForKey:@"LoginResponse"];
    NSLog(@"Login Response DICT =%@",LoginResponse);
    NSDictionary *LoginResult=[LoginResponse objectForKey:@"LoginResult"];
    NSLog(@"Login Result = %@",LoginResult);
    NSLog(@"Login Result Dict =%@",LoginResult);
    testvalue.text =[LoginResult objectForKey:@"text"];
    NSString *teststr =[[NSString alloc] init];
    teststr=testvalue.text;
    NSLog(@"Test String Value =%@",teststr);
    NSString *string = [LoginResult valueForKey:@"text"];
    NSLog(@"Now String is =%@",string);
    NSData *data =[string dataUsingEncoding:NSUTF8StringEncoding];

    NSError* error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:&error];
    NSDictionary *firstObj = [array objectAtIndex:0];

    NSString *UserCode = [firstObj valueForKey:@"UserCode"];
    NSLog(@"String value =%@",UserCode);
    NSString *userid =[firstObj valueForKey:@"UserId"];
    NSLog(@"User id is =%@",userid);
    NSString *Firstname =[firstObj valueForKey:@"FirstName"];
    NSLog(@"First Name is =%@",Firstname);
    NSString *Lastname=[firstObj valueForKey:@"LastName"];
    NSLog(@"Last Name is =%@",Lastname);
    NSString *Email =[firstObj valueForKey:@"EmailId"];
    NSLog(@"Email =%@",Email);
    NSString *Mobilenumber =[firstObj valueForKey:@"MobilNo"];
    NSLog(@"Mobile Number is =%@",Mobilenumber);

This is what I have done may be your code is different.