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]];
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:
would (IIRC) come out as
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:I know it looks like a lot of code for something this simple, but it will correctly turn
into
which sounds like what you’re looking for.