0

My php response in xml is of this type

<users>
<username>myemail</username>
<password>mypass</password>
 </users>

Now this is my parseUrl class

  NSURL *parserUrl = [[[NSURL alloc] initWithString:urlString] autorelease];
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithContentsOfURL:parserUrl] autorelease];
        [parser setDelegate:self];



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



    if ( [elementName isEqualToString:@"users"]) 
    {
        return;
    }



    if ( [elementName isEqualToString:@"username"] ) 
    {
        NSString *val = [attributeDict  objectForKey:@"username"] ;    
      return;
    }

}

Now the problem is, the attributeDict is giving 0 pairs. The function is reading username, password and users meaning it can get inside this loop

   if ( [elementName isEqualToString:@"username"] ) 
        {
            NSString *val = [attributeDict  objectForKey:@"username"] ;    
          return;
        }

But how can i retrieve the value of this node?

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

2 Answers2

1

remove return statment

and attributeDict is alway contain attributes.

e.g.

here 'id' is attribute of user.

to getting string value, you need to write

//take string variable in header file 'elementname'

NSString *elementname;

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

elementname = elementName;
}

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

    elementname = elementName;
}


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

   if([elementname isEqualToString:@"username"])
   {
       NSLog(@"username : %@",string);
   }
   else if([elementname isEqualToString:@"password"])
   {
       NSLog(@"password : %@",string);
   }
}
Romit M.
  • 898
  • 11
  • 28
  • mate can you explain by editing in my code? Your pointed function has returned me values in NSLog but how can i know if its username of password string. Kindly give an example to store these values wrt their type – Muhammad Umar Sep 18 '12 at 07:43
  • just one thing, when i run this. Its repeated two times. Firstly it shows username : xxxx then next time it shows username : (nothing) same with password. Is this some sort of bug or it should be like that? – Muhammad Umar Sep 18 '12 at 07:59
  • try this but i am not sure. i had problem but i solved that how i just not remember. [parser setShouldResolveExternalEntities:YES]; it YES/NO. – Romit M. Sep 18 '12 at 08:01
0

try this

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
    list = [[NSMutableArray alloc] init];
    resultArray = [[NSMutableArray alloc] init];
    currentElementValue = [[NSMutableString alloc] init];
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
{
currentElementName = [elementName copy];

if([elementName isEqualToString:@"username"])
{
    list = [[NSMutableArray alloc] init];
}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
if([elementName isEqualToString:@"username"])
{
    [resultArray addObject: currentElementValue];
}
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"Result Array= %@",resultArray);
NSLog(@"list Array= %@",list);
}
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41