0

i'm getting xml type of response from server but i'm unable to get the value by parsing do no where i am missing something

This was the sample Response from server :

<admin>
<logindetails 
status="cdcdvfbgfhgfgfbff" 
timestamp="1494499694240" isdaylighton="true" 
isupdateavailable="false" updateurl="" user="1" 
userParentID="0">Success</logindetails><admin>

my Parsing methods :

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

recordResults =NO;

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

    data = [soapResultsString dataUsingEncoding:NSUTF8StringEncoding];
    json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"json===>%@",array);
  }
}

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

if( recordResults )
{
    [childElement appendString: string];
    NSLog(@"inside%@",string);

}

}

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

xmlparserString=elementName;
NSLog(@"xmlparserString start -->%@",xmlparserString);

if( [xmlparserString isEqualToString:@"logindetails"])
{

    recordResults =YES;
    soapResultsString = [[NSMutableString alloc] init];

}

}

iam getting All tags name but i cant get the values from the Xml response please check the code and answer

2
  • If it comes as string you can do string operations or else you can convert them in to dictionary. In git xml to dictionary one frame work is there. Import that frame work in your application and convert xml to dictionary then you can use it as your requirement. Commented May 11, 2017 at 11:14
  • Elements are tags, the data is what is contained in the tags.... Unless you have some crazy custom weird XML, this isn't how this should go. Regardless, you're going to want to traverse tags into associative models. XML <--> JSON/Hashable Dictionary is not possible as XML can represent structures JSON cannot. Commented May 11, 2017 at 11:44

1 Answer 1

2

The data you are looking for are XML attributes and are returned in the attributes dictionary in didStartElement.

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

    xmlparserString = elementName;
    NSLog(@"xmlparserString start -->%@",xmlparserString);

    if ([xmlparserString isEqualToString:@"logindetails"]) {

        // soapResultsString = [[NSMutableString alloc] init];
        NSLog(@"logindetails attributes --> %@", attributeDict);

}

The JSON deserialization in didEndElement is wrong and cannot work. XML attributes are not JSON. Delete the entire if expression

Sign up to request clarification or add additional context in comments.

2 Comments

i got this { isdaylighton = true; isupdateavailable = false; status = "dsfdvvfbfdg"; timestamp = 1494504256966; updateurl = ""; userParentID = 0; userPrivilege = 1; } from this how to get individual values
This is a standard dictionary (key / value pairs). For example you get 1 for attributeDict[@"userPrivilege"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.