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 3460440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:13:47+00:00 2026-05-18T10:13:47+00:00

Is running a nested NSScanner the most efficient method for parsing out a string

  • 0

Is running a nested NSScanner the most efficient method for parsing out a string of repeating elements or can the scanning be done in one pass?

I have a string which is returned from a command line call (NSTAsk) to Apple’s Compressor (there are no line breaks, breaks are in purely for ease of this question being legible without scrolling):

<jobStatus name="compressor.motn" submissionTime="12/4/10 3:56:16 PM"
 sentBy="localuser" jobType="Compressor" priority="HighPriority" 
 timeElapsed="32 second(s)" timeRemaining="0" timeElapsedSeconds="32"
 timeRemainingSeconds="0" percentComplete="100" resumePercentComplete="100"
 status="Successful" jobid="CD4046D8-CDC1-4F2D-B9A8-460DF6AF184E" 
 batchid="0C9041F5-A499-4D00-A26A-D7508EAF3F85" /jobStatus>

These repeat in the same string thus there could be zero through n of these in the return string:

<jobstatus .... /jobstatus><jobstatus .... /jobstatus>
<jobstatus .... /jobstatus>

In addition there could be other tags included which are of no significance to my code (batchstatus in this example):

<jobstatus .... /jobstatus><batchstatus .... /batchstatus>
<jobstatus .... /jobstatus>

This is NOT an XML document that gets returned, merely a series of blocks of status which happen to be wrapped in an XML like tag. None of the blocks are nested. They are all sequential in nature. I have no control over the data being returned.

My goal (and currently working code) parses the string into “jobs” that contain dictionaries of the details within a jobstatus block. Any other blocks (such as batchstatus) and any other strings are ignored. I am only concerned with the contents of the jobstatus blocks.

NSScanner * jobScanner = [NSScanner scannerWithString:dataAsString];
NSScanner * detailScanner = nil;

NSMutableDictionary * jobDictionary = [NSMutableDictionary dictionary];
NSMutableArray * jobsArray = [NSMutableArray array];

NSString * key = @"";
NSString * value = @"";

NSString * jobStatus = @"";

NSCharacterSet * whitespace = [NSCharacterSet whitespaceCharacterSet];

while ([jobScanner isAtEnd] == NO) {

    if ([jobScanner scanUpToString:@"<jobstatus " intoString:NULL] &&
        [jobScanner scanUpToCharactersFromSet:whitespace intoString:NULL] &&
        [jobScanner scanUpToString:@" /jobstatus>" intoString:&jobStatus]) {

        detailScanner = [NSScanner scannerWithString:jobStatus];

        [jobDictionary removeAllObjects];

        while ([detailScanner isAtEnd] == NO) {

            if ([detailScanner scanUpToString:@"=" intoString:&key] &&
                [detailScanner scanString:@"=\"" intoString:NULL] &&
                [detailScanner scanUpToString:@"\"" intoString:&value] &&
                [detailScanner scanString:@"\"" intoString:NULL]) {

                [jobDictionary setObject:value forKey:key];

                //NSLog(@"Key:(%@) Value:(%@)", key, value);
            }
        }

        [jobsArray addObject:
         [NSDictionary dictionaryWithDictionary:jobDictionary]];
    }

}

NSLog(@"Jobs Dictionary:%@", jobsArray);

The above code produces the following log output:

Jobs Dictionary:(
    {
    batchid = "0C9041F5-A499-4D00-A26A-D7508EAF3F85";
    jobType = Compressor;
    jobid = "CD4046D8-CDC1-4F2D-B9A8-460DF6AF184E";
    name = "compressor.motn";
    percentComplete = 100;
    priority = HighPriority;
    resumePercentComplete = 100;
    sentBy = localuser;
    status = Successful;
    submissionTime = "12/4/10 3:56:16 PM";
    timeElapsed = "32 second(s)";
    timeElapsedSeconds = 32;
    timeRemaining = 0;
    timeRemainingSeconds = 0;
}

Here’s the concern. In my code I am scanning through the string and then when I get a block of data, scanning through that piece to create a dictionary that populates an array. This effectively means the string gets walked twice. As this is something that happens every 15 – 30 seconds or so and could contain hundreds of jobs, I see this as a potential CPU and memory hog and being as the app running this could be on the same machine as the Compressor app (which is already a memory and CPU hog) – I don’t want to add any burden if I don’t have to.

Is there a better way that I should be using NSScanner as I walk through it to get the data?

Any advice or recommendation much appreciated!

  • 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-05-18T10:13:48+00:00Added an answer on May 18, 2026 at 10:13 am

    Your nesting is all right in that you’re constructing detailScanner with jobStatus that jobScanner scanned. That’s not a problem. You have two others, though. One is that you’re sweating whitespace characters too much, but worse than that, your outermost loop is never going to exit because of the way your initial if conditional is formed.

    Change

    if ([jobScanner scanUpToString:@"<jobstatus " intoString:NULL] &&
    [jobScanner scanUpToCharactersFromSet:whitespace intoString:NULL] &&
    [jobScanner scanUpToString:@" /jobstatus>" intoString:&jobStatus])
    

    to

    if ([jobScanner scanString:@"<jobstatus" intoString:NULL] && 
    [jobScanner scanUpToString:@"/jobstatus>" intoString:&jobStatus] && 
    [jobScanner scanString:@"/jobstatus>" intoString:NULL])
    

    Of course, you can remove your line in which you cache your whitespace character set. You don’t need to scan whitespace characters and you don’t need to include them in the strings you scan or scan up to. By default, scanners skip whitespace characters. Uncommenting your first NSLog statement bears this out; there aren’t any stray spaces anyplace in the output.

    But you do need, once you’ve scanned up to a given string, to scan that string itself or you’re not going to move forward toward the end for your next iteration.

    Other than that, I think your approach is sound.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have some nested lists (only one level deep) and I'm running into
I was wondering what best practices were in java when running a nested class
When I'm running my program, I get the following error: ... nested exception is
I have some nested movieclips in my banner and can't realize how to prevent
I have a menu running off of a sitemap which one of the SiteMapNode
Running a rails site right now using SQLite3. About once every 500 requests or
running git instaweb in my repository opens a page that says 403 Forbidden -
Running into a problem where on certain servers we get an error that the
Running FxCop on my code, I get this warning: Microsoft.Maintainability : 'FooBar.ctor is coupled
Running OS X Leopard an MacBook Pro from Jan. 2008. I used to run

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.