title: Using NSXMLParser initWithStream: no parser delegate methods received
body: <p>The basic problem I'm working on is using the <code>NSStream</code> classes to parse incoming incremental XML data. The data is never a complete XML Document, but I want to receive and process it in incremental chunks based off how much ever the socket can read.</p>
  
  <p>Looking at the documentation for <code>NSXMLParser</code>, it seems like the <code>initWithStream:</code> method to initialize a <code>NSXMLParser</code> would be the perfect solution to my problem. I can initialize the parser with a <code>NSInputStream</code> and then call the <code>parse</code> method on <code>NSXMLParser</code> whenever I receive data over my socket which should in turn call the <code>NSXMLParser</code> delegates.</p>
  
  <p>However, I'm not seeing any of the delegates being called, the only method I see being called is the stream delegate <code>stream:handleEvent:</code>. There seems to be little to no examples of this API from Apple or other developers. Any ideas on what I'm doing wrong or how to use <code>initWithStream:</code> correctly? </p>
  
  <p><strong>ContentParser.h</strong></p>
  
  <pre><code>@interface ContentParser : NSObject &lt;NSStreamDelegate, 
                                       NSXMLParserDelegate&gt;
  {
     NSInputStream *inputStream;
     NSOutputStream *outputStream;
     NSMutableData *receivedData;
     NSXMLParser *xmlParser;
  }
  - (void)initStream;
  </code></pre>
  
  <p><strong>ContentParser.m</strong></p>
  
  <pre><code>@implementation ContentParser
  
  - (void)initStream
  {    
     CFReadStreamRef readStream;
     CFWriteStreamRef writeStream;
  
     CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                       (CFStringRef)@"&lt;hostname&gt;", 
                                       &lt;port&gt;, 
                                       &amp;readStream, 
                                       &amp;writeStream);
  
     inputStream = (__bridge NSInputStream *)readStream;
     outputStream = (__bridge NSOutputStream *)writeStream;
  
     inputStream.delegate = self;
     outputStream.delegate = self;
  
     [inputStream  scheduleInRunLoop:[NSRunLoop currentRunLoop]
                             forMode:NSDefaultRunLoopMode];
     [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                             forMode:NSDefaultRunLoopMode];
  
     [inputStream open];
     [outputStream open];
  
     xmlParser = [[NSXMLParser alloc] initWithStream:inputStream];
     [xmlParser setDelegate:self];
  }
  
  - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                          namespaceURI:(NSString *)namespaceURI 
                                         qualifiedName:(NSString *)qName 
                                            attributes:(NSDictionary *)attributeDict
  {
     NSLog(@"didStartElement: %@, attributeDict: %@", elementName, attributeDict);
  }
  
  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  {
     NSLog(@"foundCharacters: %@", string);
  }
  
  - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                                        namespaceURI:(NSString *)namespaceURI 
                                       qualifiedName:(NSString *)qName
  {
     NSLog(@"didEndElement: %@", elementName);
  }
  
  - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
  {
     NSLog(@"Error %ld, Description: %@, Line: %ld, Column: %ld", 
        [parseError code], [[parser parserError] localizedDescription], 
        [parser lineNumber], [parser columnNumber]);
  }
  
  
  - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
  {
     switch (eventCode) {
         case NSStreamEventHasBytesAvailable:
         {
             if (stream == inputStream) {
                 NSInputStream *is = (NSInputStream *)stream;
                 if (receivedData == nil) {
                     receivedData = [[NSMutableData alloc] init];
                 }
  
                 uint8_t buf[1024];
                 NSInteger bytesRead = [is read:buf maxLength:1024];
                 if (bytesRead == -1) {
                    NSLog(@"Network read error");
                 } else if (bytesRead == 0) {
                    NSLog(@"No buffer received");
                 } else {
                    [receivedData appendBytes:buf length:bytesRead];
                    BOOL parserResult = [xmlParser parse];
                    if (parserResult == NO) {
                       NSLog(@"Unable to parse XML");
                    }
                 }
             }
             break;
         }
         default:
            break;
      }
  }
  
  @end
  </code></pre>