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

  • SEARCH
  • Home
  • 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 4627086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:30:57+00:00 2026-05-22T03:30:57+00:00

I have an XML parser and i want to trim whitespace and new lines

  • 0

I have an XML parser and i want to trim whitespace and new lines before it goes to the app delegate. I know it only works with string, but how to do it for the elements inside the object. More important is it smart to do this or is it better to do a separate trimming

newString =[menu.enable stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  • 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-22T03:30:57+00:00Added an answer on May 22, 2026 at 3:30 am

    I’ve had my run ins with this issue myself, and it’s not trivial. SriPriya’s solution works but only if there are no newlines in the element content. This:

    <foo>
        hello
        hi
    </foo>
    

    would (IIRC) come out as

    @"hello\n    hi"
    

    when trimmed that way.

    The solution I came up with to solve this (and there may be more elegant solutions out there – I’m all ears) is the following:

    Presuming you’re using NSXMLParser with a delegate class that handles the actual parsing (as in SriPriya’s example above), where the -parser:foundCharacters: method is located, you would do:

    - (NSString *)removeNewlinesAndTabulation:(NSString *)fromString appending:(BOOL)appending
    {
        NSArray *a = [fromString componentsSeparatedByString:@"\n"];
        NSMutableString *res = [NSMutableString stringWithString:appending ? @" " : @""];
        for (NSString *s in a) {
            s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if (s.length > 0 
                && res.length > (appending ? 1 : 0)) [res appendString:@" "];
            [res appendString:s];
        }
        return res;
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    {   
        if (! currentElementValue) {
            currentElementValue = [[NSMutableString alloc] initWithString:[self removeNewlinesAndTabulation:string appending:NO]];
        } else {
            [currentElementValue appendString:[self removeNewlinesAndTabulation:string appending:currentElementValue.length > 0]];
        }
    }
    

    I know it looks like a lot of code for something this simple, but it will correctly turn

    <foo>
      hi there all
      i am typing some
         stuff
    </foo>
    

    into

    @"hi there all i am typing some stuff"
    

    which sounds like what you’re looking for.

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

Sidebar

Related Questions

I have heard a lot about XML parser but I want to know i.e
I have a problem with a SAX xml parser. I want to parse a
So I'm writing an android app, and I have an xml parser class that
I want to parse a simple string using XML::Parser. This works fine. But I
I have a xml file with this: <name>Alberto</name> <subname>Martín</subname> <age>24</age> I want parser it
I have XML in the following form that I want to parse with PHP
I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I have some XML that I want to parse using the lxml method in
I have an XML file and parse it into my PHP document. I want
I want to parse a large XML file and I have two options: Perl

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.