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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:39:27+00:00 2026-05-29T23:39:27+00:00

I have an XML string that gets returned to me, sometimes it is invalid

  • 0

I have an XML string that gets returned to me, sometimes it is invalid using numbers as node names such as: <2>. I would like to scan my entire NSString which holds the XML, and search for the following:

<numeric value // e.g. <1  or <2

</numeric value // e.g. </1 or </2

I would then like to place an underscore before the number, so that it will change the invalid, to valid XML, like the following:

<_2>
</_2>

I am wondering is NSScanner would do the job, but I am unsure how to attack this problem. Right now I am just using stringByReplacingOccurrencesOfString:withString: but I am having to hardcode in the number values to replace, which I don’t think is a good idea.

UPDATE:

I gave it a try and used NSRange. Here is what I came up with. It is working about 95%, but on large xml strings it misses the last few </ > tags, not sure why. Any comments or help on improving this?

// Changeable string
NSMutableString *editable = [[[NSMutableString alloc] initWithString:str] autorelease];

// Number Formatter
NSLocale *l_en = [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US"] autorelease];
NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setLocale: l_en];

// Make our first loop
NSUInteger count = 0, length = [str length];
NSRange range = NSMakeRange(0, length); 
while(range.location != NSNotFound) {

    // Find first character
    range = [str rangeOfString: @"<" options:0 range:range];

    // Make sure we have not gone too far
    if (range.location+1 <= length) {

        // Check the digit after this
        NSString *after = [NSString stringWithFormat:@"%c", [str characterAtIndex:range.location+1]];

        // Check if we return the number or not
        if ([f numberFromString:after]) {

            // Update the string
            [editable insertString:@"_" atIndex:(range.location+1)+count];
            count++;

        }//end

    }//end

    // Check our range
    if(range.location != NSNotFound) {
        range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
    }//end

}//end

// Our second part
NSUInteger slashLength = [editable length];
NSRange slashRange = NSMakeRange(0, slashLength); 
while(slashRange.location != NSNotFound) {

    // Find first character
    slashRange = [editable rangeOfString: @"</" options:0 range:slashRange];

    // Make sure we have not gone too far
    if (slashRange.location+2 <= slashLength) {

        // Check the digit after this
        NSString *afterSlash = [NSString stringWithFormat:@"%c", [editable characterAtIndex:slashRange.location+2]];

        // Check if we return the number or not
        if ([f numberFromString:afterSlash]) {

            // Update the string
            [editable insertString:@"_" atIndex:(slashRange.location+2)];

        }//end

    }//end

    // Check our range
    if(slashRange.location != NSNotFound) {
        slashRange = NSMakeRange(slashRange.location + slashRange.length, slashLength - ((slashRange.location+2) + slashRange.length));
    }//end

}//end

NSLog(@"%@", editable);
  • 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-29T23:39:29+00:00Added an answer on May 29, 2026 at 11:39 pm

    I ended up figuring out a solution. Here is the method that I used:

    - (NSString *)insertUnderscoreInString:(NSString *)fullString afterString:(NSString *)afterString {
    
        // Changeable string
        NSMutableString *editable = [[[NSMutableString alloc] initWithString:fullString] autorelease];
    
        // Number Formatter
        NSLocale *l_en = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
        NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
        [f setLocale: l_en];
        [l_en release];
    
        // Make our loop
        NSUInteger count = 0, length = [fullString length];
        NSRange range = NSMakeRange(0, length); 
        while(range.location != NSNotFound) {
    
            // Find first character
            range = [fullString rangeOfString:afterString options:0 range:range];
    
            // Make sure we have not gone too far
            if (range.location+1 <= length) {
    
                // Check the digit after this
                NSString *after = [NSString stringWithFormat:@"%c", [fullString characterAtIndex:range.location+afterString.length]];
    
                // Check if we return the number or not
                if ([f numberFromString:after]) {
    
                    // Update the string
                    [editable insertString:@"_" atIndex:(range.location+afterString.length)+count];
                    count++;
    
                }//end
    
            }//end
    
            // Check our range
            if(range.location != NSNotFound) {
                range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            }//end
    
        }//end
    
        return editable;
    
    }//end
    

    So, then you could test it using:

    NSString *val = [self insertUnderscoreInString:str afterString:@"<"];
    NSString *val2 = [self insertUnderscoreInString:val afterString:@"</"];
    NSLog(@"%@", val2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that represents a non indented XML that I would like
I have an asp page that accesses c++ code and gets a xml string
I have a function that outputs an XML string: <expensesAC> <cashflow> <month>6</month> <cash>300</cash> <projected>null</projected>
I have an object that can build itself from an XML string, and write
I have an xml document object that I need to convert into a string.
I have a helper class pulling a string from an XML file. That string
I have mysql table that has a column that stores xml as a string.
I have a web service that returns an xml string as results. The return
I have a function that gets a string passed to it. When testing the
I have a CSV string that is embedded within an XML document, which is

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.