title: iOS - parse xml DATASET
body: <p>This is my XML data that I need to parse</p>
  
  <pre><code> &lt;GetMessagesResult&gt;
      &lt;NewDataSet xmlns=""&gt;
         &lt;Table&gt;
            &lt;Date&gt;21:52:59&lt;/Date&gt;
            &lt;Message&gt;ABC&lt;/Message&gt;
            &lt;GroupName&gt;ALL&lt;/GroupName&gt;
         &lt;/Table&gt;
         &lt;Table&gt;
            &lt;Date&gt;11:23:27&lt;/Date&gt;
            &lt;Message&gt;DEF&lt;/Message&gt;
            &lt;GroupName&gt;ALL&lt;/GroupName&gt;
         &lt;/Table&gt;
     &lt;/NewDataSet&gt;
   &lt;/GetMessagesResult&gt;
  </code></pre>
  
  <p>This is my SCMessages.h file</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  @interface SCMessages : NSObject
  {
      NSDate *Date;
      NSString *Message;
      NSString *GroupName;
  }
  @property (nonatomic, retain) NSDate *Date;
  @property (nonatomic, retain) NSString *Message;
  @property (nonatomic, retain) NSString *GroupName;
  </code></pre>
  
  <p>and this is my SCMessages.m file</p>
  
  <pre><code>#import "SCMessages.h"
  @implementation SCMessages
  @synthesize Date;
  @synthesize Message,GroupName;
  - (void)dealloc
  {
      [super dealloc];
      [Date release];
      [Message release];
      [GroupName release];
  }
  @end
  </code></pre>
  
  <p>I used below code to parse the data using <code>NSXMLParser</code> delegate methods</p>
  
  <pre><code>#pragma mark - NSXMLParser Delegate
  
  -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
     attributes: (NSDictionary *)attributeDict
  {
      if( [elementName isEqualToString:@"GetMessagesResult"])
      {
          if(!soapResults)
          soapResults = [[NSMutableString alloc] init];
          recordResults = TRUE;
      }
  
      if([elementName isEqualToString:@"NewDataSet"]) {
          //Initialize the array.
          messages = [[NSMutableArray alloc] init];
      }
      else if([elementName isEqualToString:@"Table"]) {
          //Initialize the message.
          aMessage = [[SCMessages alloc] init];
      }
  }
  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  {
      if( recordResults )
          [soapResults appendString: string];
  }
  -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  {
      if([elementName isEqualToString:@"Table"]) {
          [messages addObject:aMessage];
              NSLog(@"MESSAGES COUNT: %d",messages.count);
              NSLog(@"MESSAGE: %@",aMessage);
          [aMessage release];
          aMessage = nil;
      }
      // as this is the last element
      if( [elementName isEqualToString:@"NewDataSet"])
      {
          recordResults = FALSE;
      }
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong> <em>I am not getting the desired message object with Date, Meesage &amp; GroupName.</em></p>
  
  <p>I put <code>NSLog</code> to print them but I always get null value. The weird thing is message array gets allocated memory &amp; also elements gets added to array as I can see message array count in <code>NSLog</code> but the array element has data as null value.</p>
  
  <p>I am parsing the XML data received in SOAP response in NSURLConnection delegate method <code>connectionDidFinishLoading</code></p>
  
  <pre><code>NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
      NSLog(@"theXML: \n%@",theXML);
  </code></pre>