Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:44:38+00:00 2026-06-04T23:44:38+00:00

The basic problem I’m working on is using the NSStream classes to parse incoming

  • 0

The basic problem I’m working on is using the NSStream 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.

Looking at the documentation for NSXMLParser, it seems like the initWithStream: method to initialize a NSXMLParser would be the perfect solution to my problem. I can initialize the parser with a NSInputStream and then call the parse method on NSXMLParser whenever I receive data over my socket which should in turn call the NSXMLParser delegates.

However, I’m not seeing any of the delegates being called, the only method I see being called is the stream delegate stream:handleEvent:. 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 initWithStream: correctly?

ContentParser.h

@interface ContentParser : NSObject <NSStreamDelegate, 
                                     NSXMLParserDelegate>
{
   NSInputStream *inputStream;
   NSOutputStream *outputStream;
   NSMutableData *receivedData;
   NSXMLParser *xmlParser;
}
- (void)initStream;

ContentParser.m

@implementation ContentParser

- (void)initStream
{    
   CFReadStreamRef readStream;
   CFWriteStreamRef writeStream;

   CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                     (CFStringRef)@"<hostname>", 
                                     <port>, 
                                     &readStream, 
                                     &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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:44:41+00:00Added an answer on June 4, 2026 at 11:44 pm

    I figured out what the problem was and answering it here incase anyone else runs into this problem in the future since +[NSXMLParser initWithStream] doesn’t have a lot lot of documentation out there.

    I needed to call -[NSXMLParser parse] right after I allocate NSXMLParser and set myself as delegate. But because it’s a synchronous function, I need to call it another thread so I don’t block the current thread and it can receive the NSStream events. I also don’t need to make myself the delegate for NSInputStream.

    This can be done pretty simply using Grand Central Dispatch (GCD) like so:

    // alloc and init the xml parser
    xmlParser = [[NSXMLParser alloc] initWithStream:inputStream];
    [xmlParser setDelegate:self];
    
    // block to execute
    dispatch_block_t dispatch_block = ^(void)
    {
        [xmlParser parse];
    };
    
    // create a queue with a unique name
    dispatch_queue_t dispatch_queue = dispatch_queue_create("parser.queue", NULL);
    
    // dispatch queue
    dispatch_async(dispatch_queue, dispatch_block);
    
    // cleanup
    dispatch_release(dispatch_queue);
    

    And here is the complete working example, just incase anyone wasn’t able to follow what I posted above.

    ContentParser.h

    @interface ContentParser : NSObject <NSStreamDelegate, 
                                         NSXMLParserDelegate>
    {
       NSInputStream *inputStream;
       NSOutputStream *outputStream;
       NSMutableData *receivedData;
       NSXMLParser *xmlParser;
    }
    - (void)initStream;
    

    ContentParser.m

    @implementation ContentParser
    
    - (void)initStream
    {    
       CFReadStreamRef readStream;
       CFWriteStreamRef writeStream;
    
       CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                         (CFStringRef)@"<hostname>", 
                                         <port>, 
                                         &readStream, 
                                         &writeStream);
    
       inputStream = (__bridge NSInputStream *)readStream;
       outputStream = (__bridge NSOutputStream *)writeStream;
    
       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];
    
       dispatch_block_t dispatch_block = ^(void)
       {
          [xmlParser parse];
       };
       dispatch_queue_t dispatch_queue = dispatch_queue_create("parser.queue", NULL);
       dispatch_async(dispatch_queue, dispatch_block);
       dispatch_release(dispatch_queue);
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                            namespaceURI:(NSString *)namespaceURI 
                                           qualifiedName:(NSString *)qName 
                                          attributes:(NSDictionary *)attributeDict
    {
       dispatch_block_t dispatch_block = ^(void)
       {
          NSLog(@"didStartElement: %@, attributeDict: %@", 
             elementName, attributeDict);
       };
       dispatch_queue_t main_queue = dispatch_get_main_queue();
       dispatch_async(main_queue, dispatch_block);
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
       dispatch_block_t dispatch_block = ^(void)
       {
          NSLog(@"foundCharacters: %@", string);
       };
       dispatch_queue_t main_queue = dispatch_get_main_queue();
       dispatch_async(main_queue, dispatch_block);
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                                          namespaceURI:(NSString *)namespaceURI 
                                         qualifiedName:(NSString *)qName
    {
       dispatch_block_t dispatch_block = ^(void)
       {
          NSLog(@"didEndElement: %@", elementName);
       };
       dispatch_queue_t main_queue = dispatch_get_main_queue();
       dispatch_async(main_queue, dispatch_block);
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
    {
       dispatch_block_t dispatch_block = ^(void)
       {
          NSLog(@"Error %ld, Description: %@, Line: %ld, Column: %ld", 
             [parseError code], [[parser parserError] localizedDescription], 
             [parser lineNumber], [parser columnNumber]);
       };
       dispatch_queue_t main_queue = dispatch_get_main_queue();
       dispatch_async(main_queue, dispatch_block);
    }   
    
    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
    {
       switch (eventCode) {
          case NSStreamEventHasSpaceAvailable:
          {
             /* write bytes to socket */
             break;
          }
          default:
             break;
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck on a really, really basic problem: Using HttpRequest to POST a wee
I think the following is a basic problem. I using this number Picker as
I'm having a very basic problem. My code isn't working because it isn't recognizing
The basic problem I have here is that I have one xml file that
I'm having some basic problem using pyparsing. Below is the test program and the
I have a fairly basic problem using the facebook php SDK in conjunction with
I have a pretty basic problem I can't figure out when using jQuery simplemodal:
I have decided to use Simple XML serialization and was stucked with basic problem.
Basic problem I have some large, but logically organised documents - and would like
My basic problem is to generate 2d renders of 3d objects, such as one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.