NSString+XMLExtensions.h
#import <UIKit/UIKit.h> @interface NSString(XMLExtensions) + (NSString *) encodeXMLCharactersIn:(NSString *)source; + (NSString *) decodeXMLCharactersIn:(NSString *)source; @end
NSString+XMLExtensions.m
#import "NSString+XMLExtensions.h" @implementation NSString(XMLExtensions) + (NSString *)encodeXMLCharactersIn:(NSString *)source { if (![source isKindOfClass:[NSString class]] || !source) return @""; NSString *result = [NSString stringWithString:source]; if ([result rangeOfString:@"&"].location != NSNotFound) result = [[result componentsSeparatedByString: @"&"] componentsJoinedByString: @"&"]; if ([result rangeOfString:@"<"].location != NSNotFound) result = [[result componentsSeparatedByString: @"<"] componentsJoinedByString: @"<"]; if ([result rangeOfString:@">"].location != NSNotFound) result = [[result componentsSeparatedByString: @">"] componentsJoinedByString: @">"]; if ([result rangeOfString:@"\""].location != NSNotFound) result = [[result componentsSeparatedByString: @"\""] componentsJoinedByString: @"""]; if ([result rangeOfString:@"'"].location != NSNotFound) result = [[result componentsSeparatedByString: @"'"] componentsJoinedByString: @"'"]; return result; } + (NSString *) decodeXMLCharactersIn:(NSString *)source { if (![source isKindOfClass:[NSString class]] || !source) return @""; NSString *result = [NSString stringWithString:source]; if ([result rangeOfString:@"&"].location != NSNotFound) result = [[result componentsSeparatedByString: @"&"] componentsJoinedByString: @"&"]; if ([result rangeOfString:@"<"].location != NSNotFound) result = [[result componentsSeparatedByString: @"<"] componentsJoinedByString: @"<"]; if ([result rangeOfString:@">"].location != NSNotFound) result = [[result componentsSeparatedByString: @">"] componentsJoinedByString: @">"]; if ([result rangeOfString:@"""].location != NSNotFound) result = [[result componentsSeparatedByString: @"""] componentsJoinedByString: @"\""]; if ([result rangeOfString:@"'"].location != NSNotFound) result = [[result componentsSeparatedByString: @"'"] componentsJoinedByString: @"'"]; return result; } @end