Note that there are some explanatory texts on larger screens.

plurals
  1. POiphone xml parse not working
    primarykey
    data
    text
    <p>Please do not close this topic.I really need help. I am developing on IOS 6.Xcode-4.5.2 On a button click i get the below xml data from the server</p> <pre><code>&lt;country&gt;america&lt;/country&gt; &lt;dateOfBirth xsi:nil="true"/&gt; &lt;firstName&gt;John&lt;/firstName&gt; &lt;lastName&gt;Smith&lt;/lastName&gt; &lt;userEmail&gt;johnsmith@email.com&lt;/userEmail&gt; </code></pre> <p>I am trying to parse it and get the values.I did as per the example shown in this url <a href="http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/" rel="nofollow">http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/</a> But i am getting an error when the parse method is called. Instead of the appdelegate class as shown in the example url i am using the viewcontroller class.I do not want to add any code in the Appdelegate class.</p> <pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setUserDetails:]: unrecognized selector sent to instance 0x9272a40' </code></pre> <p>I am just a beginner in iphone programming.So i need help in parsing this successfully and i just want to get the values somehow so that i can use them to show on screen in some labels. Below are codes for 6 files(Viewcontroller.h &amp; m,User.h &amp; m,Xmlparser.h &amp; m)</p> <pre><code>//ViewController.h #import &lt;UIKit/UIKit.h&gt; #import "GlobalVariable.h" #import "QuotesViewController.h" @interface ViewController : UIViewController &lt;NSXMLParserDelegate,UIApplicationDelegate,NSStreamDelegate,UIAlertViewDelegate,WriteProtocol&gt; { QuotesViewController *quoteobj; NSMutableArray *userDetails; } @property (retain, nonatomic) IBOutlet UITextField *txtUsername; @property (retain, nonatomic) IBOutlet UITextField *txtPassword; @property (retain, nonatomic) NSInputStream *inputStream; @property (retain, nonatomic) NSOutputStream *outputStream; @property (nonatomic, retain) NSMutableArray *messages; @property (retain, nonatomic) IBOutlet UILabel *label1; - (IBAction)btnLogin:(id)sender; - (IBAction)btnExit:(id)sender; - (void)initNetworkCommunication; - (void)readIn:(NSString *)s; - (void)writeOut:(NSString *)s; @property (nonatomic, retain) NSMutableArray *userDetails; @end //ViewController.m #import "ViewController.h" #import "NewTabViewController.h" #import "QuotesViewController.h" #import "XMLParser.h" @implementation ViewController @synthesize txtPassword,txtUsername,inputStream, outputStream,messages,label1,userDetails; - (void)viewDidLoad { [super viewDidLoad]; quoteobj =[[QuotesViewController alloc]init]; quoteobj.myTableView = [[UITableView alloc]init]; quoteobj.myTableView.delegate=quoteobj; quoteobj.myTableView.dataSource=quoteobj; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)btnLogin:(id)sender { NSLog(@"Clicked button1"); [self initNetworkCommunication]; NSString *response = [NSString stringWithFormat:@"POST\r\n\r\nTMS|Login|%@|%@",txtUsername.text,txtPassword.text]; NSLog(@"1"); [self writeOut:response]; } - (IBAction)btnExit:(id)sender { exit(0); } - (void) initNetworkCommunication { NSLog(@"initNetworkCommunication called"); CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"xxx.xxx.x.xx", xxxx, &amp;readStream, &amp;writeStream); inputStream = (NSInputStream *)readStream; outputStream = (NSOutputStream *)writeStream; [inputStream retain]; [outputStream retain]; [inputStream setDelegate:self]; [outputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; [outputStream open]; } - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { NSLog(@"stream event %i", streamEvent); switch (streamEvent) { case NSStreamEventOpenCompleted: NSLog(@"Stream opened"); break; case NSStreamEventHasBytesAvailable: if (theStream == inputStream) { uint8_t buffer[1024]; int len; while ([inputStream hasBytesAvailable]) { len = [inputStream read:buffer maxLength:sizeof(buffer)]; if (len &gt; 0) { NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; [self readIn:output]; if([output hasPrefix:@"JSESSIONID"]){ int len = [output length]; int lenn = len-29; sessionId = [output substringWithRange:NSMakeRange(0, lenn)]; label1.text=sessionId; NSLog(@"New String %@ LENGTH SESSUIn %i",sessionId,sessionId.length); sessionId=label1.text; NewTabViewController *so = [self.storyboard instantiateViewControllerWithIdentifier:@"newtab"]; for(UIViewController *ssa in so.viewControllers){ if([ssa isKindOfClass:[QuotesViewController class]]){ QuotesViewController *qq = (QuotesViewController *) ssa; NSLog(@"PREESENTING THIS FZZZZZZZZZZZzzaaadfsdfssdfsa"); [qq setDel:self]; } } [self presentViewController:so animated:YES completion:nil]; } } } } break; case NSStreamEventErrorOccurred: NSLog(@"Can not connect to the host!"); UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Server says.." message:@"Due to some reason server is unavailable" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; [alert show]; [alert release]; break; case NSStreamEventEndEncountered: NSLog(@"NSStreamEventEndEncountered:method is called"); [theStream close]; NSLog(@"theStream is closed"); [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; NSLog(@"theStream is removed from runloop"); [theStream release]; NSLog(@"theStream is released"); NSLog(@"Server is unavailable"); theStream = nil; if(theStream==nil){ UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Server says.." message:@"Due to some reason server is unavailable" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; [alert show]; [alert release]; } NSLog(@"IT reaches 1 here"); break; default: NSLog(@"Unknown event"); } }//End of stream -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"IT reaches here"); NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if([title isEqualToString:@"Ok"]) { NSLog(@"Ok is clicked"); exit(0); } } - (void) messageReceived:(NSString *)message { NSLog(@"Entered MessageRecieved"); [messages addObject:message]; } - (void)readIn:(NSString *)s { NSLog(@"%@", s); if ([s hasPrefix:@"{"]) { if([s rangeOfString:@"instrSymbol"].location ==NSNotFound){ NSLog(@"Received a data which is not instrumentid"); } else{ NSLog(@"JSON DATA RECEIVED"); [quoteobj parseJsonData:s]; } } else if([s hasPrefix:@"&lt;"]){ NSLog(@"XML DATA RECEIVED"); NSData *xmlData= [[NSData alloc]initWithData:[s dataUsingEncoding:NSUTF8StringEncoding]]; NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData]; XMLParser *parser = [[XMLParser alloc] initXMLParser]; //Set delegate [xmlParser setDelegate:parser]; //Start parsing the XML file. BOOL success = [xmlParser parse]; if(success) NSLog(@"No Errors"); else NSLog(@"Error Error Error!!!"); } } - (void)writeOut:(NSString *)s { if (outputStream) { NSLog(@"WRITING OUT"); uint8_t *buf = (uint8_t *)[s UTF8String]; [outputStream write:buf maxLength:strlen((char *)buf)]; NSLog(@"Writing out the following:"); NSLog(@"%@", s); } else{ NSLog(@"Noutoyt"); } } @end //XMLParser.h #import &lt;Foundation/Foundation.h&gt; @class ViewController,User; @interface XMLParser : NSObject{ NSMutableString *currentElementValue; ViewController *zappDelegate; User *aBook; } - (XMLParser *) initXMLParser; @end //XMLParser.m #import "XMLParser.h" #import "ViewController.h" #import "User.h" @implementation XMLParser - (XMLParser *) initXMLParser { [super init]; zappDelegate = (ViewController *)[[UIApplication sharedApplication] delegate]; return self; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"country"]) { //Initialize the array. zappDelegate.userDetails = [[NSMutableArray alloc] init]; } else if([elementName isEqualToString:@"firstName"]) { //Initialize the book. aBook = [[User alloc] init]; //Extract the attribute here. //aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue]; //NSLog(@"Reading id value :%i", aBook.bookID); } NSLog(@"Processing Element: %@", elementName); } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(!currentElementValue) currentElementValue = [[NSMutableString alloc] initWithString:string]; else [currentElementValue appendString:string]; NSLog(@"Processing Value: %@", currentElementValue); } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"Books"]) return; if([elementName isEqualToString:@"Book"]) { [zappDelegate.userDetails addObject:aBook]; [aBook release]; aBook = nil; } else [aBook setValue:currentElementValue forKey:elementName]; [currentElementValue release]; currentElementValue = nil; } @end //User.h #import &lt;Foundation/Foundation.h&gt; @interface User : NSObject { //NSInteger bookID; NSString *firstName; //Same name as the Entity Name. NSString *lastName; //Same name as the Entity Name. NSString *userEmail; //Same name as the Entity Name. } @property (nonatomic, retain) NSString *firstName; @property (nonatomic, retain) NSString *lastName; @property (nonatomic, retain) NSString *userEmail; @end //User.m #import "User.h" @implementation User @synthesize firstName,lastName,userEmail; - (void) dealloc { [firstName release]; [lastName release]; [userEmail release]; [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload