Re: XML-/HTML-safe formatted output from NSString
Re: XML-/HTML-safe formatted output from NSString
- Subject: Re: XML-/HTML-safe formatted output from NSString
- From: Scott Anguish <email@hidden>
- Date: Tue, 29 Oct 2002 21:10:56 -0500
On Tuesday, October 29, 2002, at 07:07 PM, Terence Goggin wrote:
Hi all,
I have some data that I'm writing to disk via an NSString... However,
the resulting data gets spit out as XML or HTML formatted data which
means that I need a way to catch and replace any XML/HTML reserved
characters such as <, <, &, etc.
Does anyone have any thoughts on easiest ways to accomplish this? I
could go through the NSString one character at a time and just replace
as needed, but I was hoping there was already code or methods to do
this available somewhere.
I think this escapes everything needed (if not, someone should let me
know)
@implementation NSString(youtalkintome)
- (NSString *)stringByEncodingXMLCharacters
{
NSMutableString *_theString=[self mutableCopy];
[_theString replaceOccurrencesOfString:@"&" withString:@"&"
options:NSLiteralSearch
range:NSMakeRange(0, [self
length])];
[_theString replaceOccurrencesOfString:@"<" withString:@"<"
options:NSLiteralSearch
range:NSMakeRange(0, [self
length])];
[_theString replaceOccurrencesOfString:@">" withString:@">"
options:NSLiteralSearch
range:NSMakeRange(0, [self
length])];
NSString *_nonMutableVersion=[NSString stringWithString:_theString];
[_theString release];
return _nonMutableVersion;
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.