Encode NSString for XML/HTML Encode NSString for XML/HTML ios ios

Encode NSString for XML/HTML


There isn't an NSString method that does that. You'll have to write your own function that does string replacements. It is sufficient to do the following replacements:

  • '&' => "&"
  • '"' => """
  • '\'' => "'"
  • '>' => ">"
  • '<' => "<"

Something like this should do (haven't tried):

[[[[[myStr stringByReplacingOccurrencesOfString: @"&" withString: @"&"] stringByReplacingOccurrencesOfString: @"\"" withString: @"""] stringByReplacingOccurrencesOfString: @"'" withString: @"&#39;"] stringByReplacingOccurrencesOfString: @">" withString: @">"] stringByReplacingOccurrencesOfString: @"<" withString: @"<"];


I took Mike's work and turn it into a category for NSMutableString and NSString

Make a Category for NSMutableString with:

- (NSMutableString *)xmlSimpleUnescape{    [self replaceOccurrencesOfString:@"&"  withString:@"&"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@""" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"&#x27;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"&#39;"  withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"&#x92;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"&#x96;" withString:@"-"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@">"   withString:@">"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"<"   withString:@"<"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    return self;}- (NSMutableString *)xmlSimpleEscape{    [self replaceOccurrencesOfString:@"&"  withString:@"&"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"'"  withString:@"&#x27;" options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@">"  withString:@">"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];    [self replaceOccurrencesOfString:@"<"  withString:@"<"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];    return self;}

Make a Category for NSString with:

- (NSString *)xmlSimpleUnescapeString{    NSMutableString *unescapeStr = [NSMutableString stringWithString:self];    return [unescapeStr xmlSimpleUnescape];}- (NSString *)xmlSimpleEscapeString{    NSMutableString *escapeStr = [NSMutableString stringWithString:self];    return [escapeStr xmlSimpleEscape];}

* A Swift 2.0 Version *

The Objective-C version is a little more efficient as it does mutable operations on the string. However, this is a swift way to do simple escaping:

extension String{    typealias SimpleToFromRepalceList = [(fromSubString:String,toSubString:String)]    // See http://stackoverflow.com/questions/24200888/any-way-to-replace-characters-on-swift-string    //    func simpleReplace( mapList:SimpleToFromRepalceList ) -> String    {        var string = self        for (fromStr, toStr) in mapList {            let separatedList = string.componentsSeparatedByString(fromStr)            if separatedList.count > 1 {                string = separatedList.joinWithSeparator(toStr)            }        }        return string    }    func xmlSimpleUnescape() -> String    {        let mapList : SimpleToFromRepalceList = [            ("&",  "&"),            (""", "\""),            ("&#x27;", "'"),            ("&#39;",  "'"),            ("&#x92;", "'"),            ("&#x96;", "-"),            (">",   ">"),            ("<",   "<")]        return self.simpleReplace(mapList)    }    func xmlSimpleEscape() -> String    {        let mapList : SimpleToFromRepalceList = [            ("&",  "&"),            ("\"", """),            ("'",  "&#x27;"),            (">",  ">"),            ("<",  "<")]        return self.simpleReplace(mapList)    }}

I could have used the NSString bridging capabilities to write something very similar to the NSString version, but I decided to do it more swifty.


I use Google Toolbox for Mac (works on iPhone). In particular, see the additions to NSString in GTMNSString+HTML.h and GTMNSString+XML.h.


matomo