49052

cant retrieve xml values using NSXMLParser

Question:

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?

Answer1:

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); } }

Answer2:

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); }

Recommend

  • cant retrieve xml values using NSXMLParser
  • Not able to create valid nsurl
  • get date from an nsdictionary and match them
  • iPhone - Can we parse .plist(xml) file using NSXMLParser? [duplicate]
  • NSXMLParserErrorDomain error 31
  • How to choose between two elements of the same name when parsing xml
  • Can't Send NSMutableDictionary To Another Class
  • Apple rejected app due to a crash which is not reproducing
  • problem in copy string for other class
  • -[__NSCFString objectForKey:]: unrecognized selector sent to instance issue
  • encode arabic letter unichar to regular letters
  • Displaying content from an RSS feed in an iphone app
  • NSXMLParser doesn't ignore CDATA
  • How to parse & print get a element by element from xml in objective C? [closed]
  • How to get XML namespace?
  • Modifying Existing XML Content in C#
  • Parse elements with the same name?
  • NSXMLParser with multiple attributes
  • Cython speed vs numpy
  • Not able to stretch an inner StackPanel
  • UITableView gives empty table, does not load data
  • How to make text wrap in a WPF TreeViewItem?
  • Tutorial or sample code for parsing xml file using libxml2 in IPhone Application
  • UIAlertView Pops Up Three Times per Call Instead of Just Once
  • Class works not correct. How to make it better?
  • Binding a list with another list with WPF
  • Post comment to WordPress Blog from iPhone programmatically
  • iOS Date formatting
  • Different Datacontext for Command and CommandParameter
  • What is this iOS exception with NSCFType?
  • how to solve invalid conversion specifier warning in iphone app
  • Is it possible to run clang with llc flags
  • Adding elements to a huge XML file
  • How integrated is Collada to OpenGL ES
  • Jackson Parser: ignore deserializing for type mismatch
  • Compare two NSDates in iPhone
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?