title: iOS: Combining SAX and DOM parsing
body: <p>I am currently working on an iPad project for which I need to process large XML file into an SQLite backend. I currently have this working using the <a href="http://tbxml.co.uk/TBXML/TBXML_Free.html" rel="nofollow">TBXML</a> parser.</p>
  
  <p>So all the logic is in place and in general the TBXML parser does the job it needs to do. Only problem I'm now encountering is that the XML files are getting too big and I am running out of memory. Because of this I thinking of switching to a SAX parser like the core NSXMLParser of something like Alan Quatermain's <a href="http://quatermain.tumblr.com/post/93651539/aqxmlparser-big-memory-win" rel="nofollow">AQXMLParser</a>. However this will require me to redo all of my current logic that to some extent relies on functions provided by a DOM tree. This is something I'd rather not do.</p>
  
  <p>So what I want to try and do is create a hybrid approach. Given my XML structure this should be possible. It's basically a number of repeating "Record" elements. And within each record are various elements that can be repeating and nested. 
  In my current approach I parse the document and pass each record element to a function that processes it into the database. Given that this already exists I want to use this in my hybrid parsing approach.</p>
  
  <p>This is what I want to achieve. Using a SAX parser I traverse my document. While traversing the document I build a Record element. Whenever I complete a record element I pass it along to the existing function that uses TBXML to process it. The SAX parser then continues to build the next record element. Key goals are to:
  - Fix the memory footprint (it doesn't need to the smallest it can be, but it has to be contstant or at least smaller that using TBXML)
  - Keep performance acceptable.</p>
  
  <p>Currently want to implement this as follows:</p>
  
  <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
      //Recreate record string each time record element is encountered
      if([elementName isEqualToString:@"Record"]) record = [[NSMutableString alloc] init];
      //Write XML tag with name
      [record appendFormat@"&lt;%@&gt;, elementName];
  }
  
  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
      //Write XML content
      [record appendString:string];
  }
  
  - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
      //Write XML tag
      [record appendFormat@"&lt;/%@&gt;, elementName];
      if([elementName isEqualToString:@"Record"]){
          //Parse record string into TBXML object
          TBXML * tbxmlRecord = [TBXML tbxmlWithXMLString:record];
          //Send it to the TBXML record processor
          [self processElement:tbxmlRecord.rootXMLElement];
      }
  }
  </code></pre>
  
  <p>I think this should work but it feels dirty to use a string. Furthermore I have my concerns on if the record string won't get overwritten too soon when the parser reaches a new record element.</p>
  
  <p>So my question is, if this is a sound way to approach this or if there are better ways for me to achieve what I'm looking for?</p>
  
  <p><b>Edit:</b>
  I've implemented this approach and it looks to working quite well. Only hiccup I've encountered is that if my source file isn't UTF-8 encoded I only get a partial result. But when I correct this all goes well. Memory usage isn't that much better though. But maybe it takes what it can. Need to run more tests...</p>