title: Parse Nested XML Objective C - NSXMLParser
body: <p>All,</p>
  
  <p>I have XML in the following format:</p>
  
  <pre><code>&lt;linked-list&gt;
      &lt;Description&gt;
          &lt;desc&gt;&lt;/desc&gt;
          &lt;IP&gt;&lt;/IP&gt;
      &lt;/Description&gt;
  &lt;/linked-list&gt;
  </code></pre>
  
  <p>This XML statement could have an infinite number of &lt;Description>&lt;/Description> inside of the &lt;linked-list>&lt;/linked-list>. </p>
  
  <p>How should I parse this using NSXMLParser? My current code is as follows, but it parses incorrectly. </p>
  
  <pre><code>@implementation XMLParser
  
  @synthesize response;
  
  - (XMLParser *) initXMLParser
  {
      self = [super init];
      // init dictionary of response data 
      response = [[NSMutableDictionary alloc] init];
      return self;
  }
  
  //Gets Start Element of SessionData
  - (void)parser:(NSXMLParser *)parser 
          didStartElement:(NSString *)elementName 
          namespaceURI:(NSString *)namespaceURI 
          qualifiedName:(NSString *)qualifiedName 
          attributes:(NSDictionary *)attributeDict 
  {
      if ([elementName isEqualToString:@"linked-list"])
      {
          NSLog(@"Found linked-list in the return XML! Continuing...");
          //response is a NSMutableArray instance variable
  
          //THIS SHOULD NEVER NEED TO BE USED
          if (!response)//if array is empty, it makes it!
          {
              NSLog(@"Dictionary is empty for some reason, creating...");
              response = [[NSMutableDictionary alloc] init];
          }
          //END: THIS SHOULD NEVER BE USED
          return;
      }
      else
      {
          currentElementName = elementName;
          NSLog(@"Current Element Name = %@", currentElementName);
          return;
      }
  }
  
  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
  {
      if (!currentElementValue) {
          // init the ad hoc string with the value     
          currentElementValue = [[NSMutableString alloc] initWithString:string];
      } else {
          [currentElementValue setString:string];
          NSLog(@"Processing value for : %@", string);
      }
  }  
  
  //Gets End Element of linked-list
  - (void)parser:(NSXMLParser *)parser 
   didEndElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI 
   qualifiedName:(NSString *)qName {
      if ([elementName isEqualToString:@"linked-list"])
      {
          // We reached the end of the XML document
          // dumps dictionary into log
          NSLog(@"Dump:%@", [response description]);
          return;
      }
      else
      {
          //Adds key and object to dictionary
          [response setObject:currentElementValue forKey:currentElementName];
          NSLog(@"Set values, going around again... brb.");
      }
      currentElementValue = nil;
      currentElementName = nil;
  }
  
  
  @end
  </code></pre>